there are many post about how to prevent an ASP textbox to accept only numbers, but could somebody give me a JS example, how to check if the entered value is between 0-100?
My validator fires each time key is entered and checks if it is a number or not, but do not know how to extend it to check the final value against 0-100 range.
function onlyDotsAndNumbers(event) {
var charCode = (event.which) ? event.which : event.keyCode
if (charCode == 46) {
return true;
}
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
Cheers
A
You need to check out the whole value of the text box.
function onlyDotsAndNumbers(event) {
var charCode = (event.which) ? event.which : event.keyCode
if (charCode == 46) {
return true;
}
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
var boxValue = $(event.target).val();
if (boxValue > 100 || boxValue < 0)
return false;
return true;
}
Related
function AllowOnlyNumbers(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57)){
if (charCode === 8 && charCode === 46) {
return false;
}
}
return true;
}
How to allow only numbers and delete key or backspace to be written in this textbox ?
Why not use input of type number <input type="number" /> for browsers that supports it, otherwise use javascript:
function AllowOnlyNumbers(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}
Here is a fiddle: http://jsfiddle.net/Lm2hS/
From: https://stackoverflow.com/a/7295864/235659
I have created this solution for your problem here:
https://codebrace.com/editor/b05f92054
Here I have used event.charCode == 0 to allow non characters key pressed (To allow delete, backspace and other non-character keys) and isNaN to check if the value entered is a number or not.
I hope this helps!
I am using this code to allow only digits to type in textbox but now I want to allow . too. I modified this code but not working.
function isNumberKeyDotAllowed(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode == 46)
return false;
return true;
}
TextBox declaration in markup:
<asp:TextBox runat="server" ID="txtBoxApplicantCNICNo"
onkeypress="return isNumberKeyDotAllowed(this)" AutoPostBack=True
OnTextChanged="txtCHan_event" CssClass="form-control">
I see two problems with your code. The first is that you pass this as the argument to isNumberKeyDotAllowed while you should pass event:
onkeypress="return isNumberKeyDotAllowed(event);"
The second is the validation condition. Here is my own version of the function. I defined the condition for success instead of the condition for failure, because it is easier for me to figure out:
function isNumberKeyDotAllowed(evt) {
var charCode = evt.which ? evt.which : evt.keyCode;
if (charCode == 46 || (48 <= charCode && charCode <= 57)) {
return true;
}
else {
return false;
}
}
I want to remove first space from text field. i have created function which allow only characters.
Here is my html code :
<form:input path="deliveryBoyName" id="deliveryBoyName"
maxlength="100" class="form-control"
onkeypress="return onlyAlphabets(event,this);">
</form:input>
Here is my javascript function :
function onlyAlphabets(e, t) {
try {
if (window.event) {
var charCode = window.event.keyCode;
}
else if (e) {
var charCode = e.which;
}
else { return true; }
if (charCode == 0 || charCode == 8 || charCode == 17 || charCode == 20 || charCode == 32 || (charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123))
return true;
else
return false;
}
catch (err) {
alert(err.Description);
} }
Now if user first types space then it should remove. starts only from character.
For example :
If user types like " Hello World"
Then it should not allowed.
If user type like "Hello World" then its should allowed. please help me.
Thank you in advance.
JavaScript trim() function can remove white spaces from both the side.
Here is working fiddle -
var str = " Did you find solution Ashish? If yes then tick it.";
alert(str.trim());
I think you want to allow space only when it is not the first character.
This is what you want, i.e. your function removing all the unnecessary code:
function onlyAlphabets(e, t) {
var charCode = e ? e.which : window.event.keyCode;
return (charCode == 0 || charCode == 8 || charCode == 17 || charCode == 20 ||
(t.value.length && charCode == 32) ||
(charCode > 64 && charCode < 91) ||
(charCode > 96 && charCode < 123))
}
I'm having some trouble adding validation for stops to my input box. The restriction on allowing numbers only (second condition) works, but the first condition may as well not exist. Full stops and decimal points still do not appear in the input box.
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode == 110 || charCode == 190)
return true;
else if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
Additionally, I've noticed that the behaviour of this JS is different across browsers. in FireFox, I can use the numeric keypad to enter a value. However, in Chrome I am limited to the top row of numbers. Neither browser allows decimal points.
onkeypress won't report the key codes, it will report the ASCII character typed. You want onkeydown
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode == 110 || charCode == 190)
return true;
else if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
document.getElementById('foo').onkeydown = isNumberKey;
<input type="text" id="foo" />
Statement: I have a input field.User enters only Numbers in it. Max allowed value is 500.
So if user tries to type a value greater than 500 he should not be able to type the value.
For example:
Hundreds place max value would be 5 if user tries to enter 600 or 700.
Tens and Units place Max value would be 0 if user has typed 5 as the first digit(hundreds place)
PS : The handling for user entering only numbers is done already using the following code snippet:
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
};
Fix for the Problem:
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
if(!(charCode > 31 && (charCode < 48 || charCode > 57)) && charCode != 8){
if($(evt.currentTarget).value.length == 3){
return false;
}
if($(evt.currentTarget).value.length == 2){
if($(evt.currentTarget).value.substring(0,1) > 5){
return false;
}else if($(evt.currentTarget).value.substring(0,1) == 5 && $(evt.currentTarget).value.substring(1,2) > 0 ){
return false;
}
}
}
return true;
};
As i understand you can use onchange instead of keypress:
$('#textbox').change( function(){
elem = $(this);
if(parseInt(elem.val()) > 500)
elem.val('500');
});