Tab and delete using javascript is not working in mozilla - javascript

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

Related

ASCII code not working in firefox but on chrome working fine

I had created function which allows to enter all characters and almost all symbols on chrome and that is working fine but on firefox I am not able to enter any character or any symbols
Below is my script
function blockSpecialChar(e) {
var k = e.keyCode;
return ((k > 64 && k < 91) || (k > 96 && k < 123) || (k > 31 && k < 34) || (k > 34 && k < 38) || k == 8 || k == 64 || k == 95 || k == 61 || k == 63 || (k > 39 && k <= 57));
}
.aspx code
<asp:TextBox ID="txtdescribe" onkeypress="return blockSpecialChar(event)" CssClass="input" ValidationGroup="services" runat="server" MaxLength="255"></asp:TextBox>
This function restricts only html symbols to insert but on firefox it is not allowing to insert character also
Example on chrome
Example on firefox
Try this:
function blockSpecialChar(e) {
if (e == 60 || e == 62 || e == 34 || e == 38 || e == 39) {
return false;
}
else {
return true;
}
}
onClick="ToggelDiv(this.id); Specialinstruction();"
These will be work on all browsers.

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

Allow some special character like dash in 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.

Space Bar (32) was not working at keypress

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

CheckNumeric value in JavaScript

I have this function for checking text box value for entering only integer values.
function CheckNumeric(e) {
if (window.event) // IE
{
if ((e.keyCode < 48 || e.keyCode > 57) & e.keyCode != 8) {
event.returnValue = false;
return false;
}
}
else { // Fire Fox
if ((e.which < 48 || e.which > 57) & e.which != 8) {
e.preventDefault();
return false;
}
}
}
In this function textbox can accept 0 for value and reject any character.
Now I want to change this function to reject 0 if it is entered as the first character.
For example reject this: 010 and accept this: 10
you need to check if (e.keyCode == 48 && input.value.length == 0) then return false;
input is your text box element.
something like this
if (window.event) // IE
{
if (((e.keyCode < 48 || e.keyCode > 57) & e.keyCode != 8) || (e.keyCode == 48 && input.value.length == 0)) {
event.returnValue = false;
return false;
}
}
and it should be
(e.keyCode < 48 || e.keyCode > 57) && e.keyCode != 8)
instead of
(e.keyCode < 48 || e.keyCode > 57) & e.keyCode != 8)

Categories

Resources