Javascript: Validate for only alpha and space - javascript

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);

Related

How to apply conditional return statement in JavaScript

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));
}

How to remove/delete first space of word using javascript or jquery?

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))
}

how to enable arrow keys in javascript

In my application i wrote java script validation for a text field (user name). So it allows only alphabets, spaces, back-space and arrows to move previous and next alphabets in the text field. my code is working fine in the mozila firefox but coming to chrome and IE its not allowing arrow keys.
My code is like this..
<input class="form-control input-lg" onkeypress="return isCharacterKey(event)" onkeyup="capitalize(this)" id="firstNameSpaceCapital"/>
//This function allows space,backspace,alphabets and arrow keys
function isCharacterKey(evt) {
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode == 32 || charCode == 8 || (charCode >= 37 && charCode <= 40) || (charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122)) {
return true;
}
return false;
}
//This method is used to capitalize the first letter in the text field
function capitalize(obj) {
obj.value = obj.value.charAt(0).toUpperCase() + obj.value.slice(1);
}
//This method is used to capitalize the first letter after space
$('#firstNameSpaceCapital').on('keyup', function () {
$(this).val(function (i, val) {
return val.replace(/(\s)(\S)/g, function ($0, $1, $2) {
return $1 + $2.toUpperCase();
});
});
});
I'd tackle your issue like this:
Notice for the capitalization only css is needed (for the case you presented at least)
FIDDLE
html
<input class="form-control input-lg" id="firstNameSpaceCapital" />
css
.input-lg {text-transform:capitalize;}
js
$('#firstNameSpaceCapital').on('keypress', isCharacterKey);
function isCharacterKey(evt) {
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode == 32 || charCode == 8 || (charCode >= 37 && charCode <= 40) || (charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122)) {
return true;
}
evt.preventDefault();
}
good luck!
return isCharacterKey(event)
event is undefined, use this instead.
return isCharacterKey(this)
I think it's a bad idea to catch keyboard events. 'Cause user can paste some text using mouse. So, I'd recommend to use oninput event

What does || do in ( charCode < 48 || charCode > 57)?

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.

Javascript event keypress check for Char Code

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');
});

Categories

Resources