Allow some special character like dash in Javascript - javascript

I'm using javascript to disallow user to use special character in textbox. But I want to allow user to use dash (-) in textbox. Kindly update my function.
To Dis Allow user to use special character
function alpha(e) {
var k;
document.all ? k = e.keyCode : k = e.which;
return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 32 || (k >= 48 && k <= 57));
}
Allow only numbers
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode != 46 && charCode > 31
&& (charCode < 48 || charCode > 57))
return false;
return true;
What is the keycode to allow use to use dash (-) in 1st function ?

Here is a javascript keyCode list: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes.
Dash KeyCode is 189(- from keyboard) or 109(- [minus sign - subtract from keypad]).
Dash has a CharCode of 45.
Here is a description of what KeyCode & CharCode means: keycode and charcode
And here you can test keydown, keypress, keyup events. http://www.quirksmode.org/js/keys.html
So if you're applying your alpha function on keydown event, you must change it accordingly:
function alpha(e) {
var k;
document.all ? k = e.keyCode : k = e.which;
// UPDATED - is CHARCODE
return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 32 || (k >= 48 && k <= 57) || k == 45);
}
Depends if you want minus or dash or just one of them.

Dash key code is 189.
But your code is harder to maintain with char codes, you should consider using named constants in place of numbers.

Related

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

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

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: Validate for only alpha and space

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

Categories

Resources