Space Bar (32) was not working at keypress - javascript

I need to enter only string valriable into the textbox but space bar was not working please some one help me friends. . .
$(".onlyname").keypress(function (evt) {
evt = (evt) ? evt : event;
var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
((evt.which) ? evt.which : 0));
if (charCode > 31 && (charCode == 9 || charCode == 40 || charCode == 39 || charCode == 32 || charCode == 37 || charCode == 27 || charCode < 65 || charCode > 90) &&
(charCode < 97 || charCode > 122)) {
$('#error').attr('class', 'errorMessage');
$('#error').text("Enter Only Alphabets Value");
return false;
}
else {
$('#error').attr('class', ' display: none;');
$('#error').text("");
return true;
}
});

This condition is wrong
if (charCode > 31 && (charCode == 9 || charCode == 40 || charCode == 39 || charCode == 32 || charCode == 37 || charCode == 27 || charCode < 65 || charCode > 90) &&
(charCode < 97 || charCode > 122)) {
Space bar key code is 32, so when you type space this if condition is satisified and it prints the error.
Try tis fiddle http://jsfiddle.net/nfDM8/
Or try this edit in your own code
$(".onlyname").keypress(function (evt) {
evt = (evt) ? evt : event;
var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
((evt.which) ? evt.which : 0));
if (charCode > 32 && (charCode == 9 || charCode == 40 || charCode == 39 || charCode == 37 || charCode == 27 || charCode < 65 || charCode > 90) &&
(charCode < 97 || charCode > 122)) {
alert("Enter Only Alphabets Value");
return false;
}
else {
return true;
}
});

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

text field that only allow and number and dash and also accepts ctrl commands

function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode != 45 && charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
i have been using this function but its does not allow ctrl + commands.
like ctrl + a , ctrl + c
May be you want to do something like this to sort it out:
function isNumberKey(evt) {
var charCode = evt.which || event.keyCode;
if (!evt.ctrlKey && charCode != 45 && charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
just check if ctrlKey is pressed, if do then only in conjunction with !evt.ctrlKey disable it.

Tab and delete using javascript is not working in mozilla

In the below code i have 2 js function for accepting alphabets and alphanumeric in which when i tested in mozilla firefox the tab is not workingfor alphanumeric and tab ,backspace ,delete is not working for alphabets pls anyone help me to solve the issue.
function alphanumeric(e) {
var k;
document.all ? k = e.keyCode : k = e.which;
return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 ||k == 9|| k == 32 || (k >= 48 && k <= 57));
}
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 > 64 && charCode < 91) || (charCode == 9 && charCode == 8)|| (charCode > 96 && charCode < 123))
return true;
else
return false;
}
catch (err) {
alert(err.Description);
}
}
This works in both FF and Chrome:
function alphanumeric(e) {
var k;
k = e.keyCode || e.charCode;
return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 9 || k == 32 || (k >= 48 && k <= 57));
}
function onlyAlphabets(e, t) {
try {
if (window.event) {
var charCode = window.event.keyCode;
} else if (e) {
var charCode = e.keyCode || e.charCode;
} else {
return true;
}
if ((charCode > 64 && charCode < 91) || (charCode == 9 || charCode == 8) || (charCode > 96 && charCode < 123)) return true;
else return false;
} catch (err) {
alert(err.Description);
}
}
In onlyAlphabetic()
(charCode == 9 && charCode == 8)
should be:
(charCode == 9 || charCode == 8)
It's not possible for charCode to be equal to both of them at the same time.
In the keypress event, some keys have keyCode == 0 so it's necessary to use charCode.
I suggest you read up on the difference between keypress and keydown/keyup, and charCode versus keyCode.
DEMO

How can i set more than one id in one function

i have 2 function in jquery like this :-
$('#age').on('keypress', function(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
return !(charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57));
});
$('#phone').on('keypress', function(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
return !(charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57));
});
this tow function is same to do .
so i need to mearge this tow function in one like :-
$('#phone') + $('#age').on('keypress', function(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
return !(charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57));
});
like this: just for demo sake: http://jsfiddle.net/2ASnz/
You can combine multiple selectors with a comma.
This link will give you more insight: http://api.jquery.com/multiple-selector/
Hope this help :)
code
$('#phone,#age').on('keypress', function(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
return !(charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57));
});
http://api.jquery.com/multiple-selector/
You can specify any number of selectors to combine into a single
result. This multiple expression combinator is an efficient way to
select disparate elements. The order of the DOM elements in the
returned jQuery object may not be identical, as they will be in
document order. An alternative to this combinator is the .add()
method.
try this
$('#phone,#age').on('keypress', function(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
return !(charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57));
});
Give the two elements same class and select them by their class, then do same thing to both elements.
jQuery generally works on collections. You can pass more than one selector expression separated by commas. This will work as you required.
$('#phone, #age').on('keypress', function(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
return !(charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57));
});
You can just use , in your selectors to have multiple selectors Multiple Selectors
$('#phone,#age').on('keypress', function(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
return !(charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57));
});

Categories

Resources