Can anyone tell me what the two lines do here?
( charCode < 48 || charCode > 57))
I guess it means something like "or" or "do both"...
function numberCheck(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode > 31 && ( charCode < 48 || charCode > 57))
{
document.getElementById("numonly").innerHTML = "Numbers Please!";
return false;
}
else
{
document.getElementById("numonly").innerHTML = "";
return true;
}
}
So what your saying is that the code is looking for all characters except 48-57?
Char codes 48 to 57 represent the number keys 0 - 9
|| means OR
therefore the expression will evaluate to true for any character that is not a number.
It is an "or". Seems like the code is looking for character that are NOT in the range of character code 48 through 57.
Related
I have this html input:
Rows: <input type="text" class="rows" onkeypress="return isNumber(event)"><br>
and this javascript function to validate only numbers
function isNumber(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;
}
but i want to work with micro-branching to do something like this:
function isNumber(evt){
evt = evt || window.event;
var charCode = evt.which || evt.keyCode;
(charCode > 31 && (charCode < 48 || charCode > 57)) && return false;
return true;
}
the thing is that the last 2 lines didnt work.
return is a statement rather than an expression, and thus cannot be used as argument to a logical operator.
In your case however, the last two lines can be rewritten into a single return statement, by simply inverting the condition to the if clause:
return !(charCode > 31 && (charCode < 48 || charCode > 57));
Or, as zerkms notes, you can lose the ! by flipping the operators (&& <=> || and < <=> >=), which, in my humble opinion, increases readability:
return charCode <= 31 || (charCode >= 48 && charCode <= 57);
According to your description, it looks like you are looking for conditional check and return :
function isNumber(evt){
evt = evt || window.event;
var charCode = evt.which || evt.keyCode;
return !(charCode > 31 && (charCode < 48 || charCode > 57));
}
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" />
I have this function which I use to validate for letters only when a user inputs something in a textbox.
I just realized it does not accept spaces. How would I fix this? Just add the charCode value to the equation?
function isLetter2(evt) {
evt = (evt) ? evt : event;
var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
((evt.which) ? evt.which : 0));
if (charCode > 31 && (charCode < 65 || charCode > 90) &&
(charCode < 97 || charCode > 122)) {
return false;
}
return true;
}
A space is charcode 32 (hexadecimal 20), so just add this line before your first if statement.
if (charCode == 32) return true;
followed by your original if statement. (No additional else statement is needed between them since the function will now stop and return true as soon as a space is detected.)
update: your original if statement can be simplified by De Morgan's laws to <= 31 or (inclusively between 65 and 90) or (inclusively between 97 and 122). So it would also work fine just to change your first > 31 to be > 32 since its negation would be <= 32 and result in returning of true, thus allowing a space to be typed. But I am not sure why you would want to allow all of the other characters less than 32 to be typed... that includes a lot of weird stuff. Maybe you should change your code to be like this:
if (charCode == 32 ||
(charCode >= 65 && charCode <= 90) ||
(charCode >= 97 && charCode <= 122))
return true;
else return false;
In fact, the result of all those comparisons is itself equal to the value true or the value false, and so you can completely eliminate the if/else statements and just say:
return charCode == 32 || (charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122);
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');
});