Javascript function to avoid special characters. It is working as expected with alpha & numbers.only problem is, when i need to include -,. (k >= 188 && k <= 190) - that is not working. what am i doing wrong?
var k;
document.all ? k = e.keyCode : k = e.which;
return ((k > 64 && k < 91) || (k > 96 && k < 123) || (k >= 188 && k <= 190)|| k == 8 || k == 32 || (k >= 48 && k <= 57));
This is my latest code,it will support all the browser.
function isAlphaNumeric(evtGet) {
var browser = navigator.appName;
if (browser == "Microsoft Internet Explorer" || browser == "Mozilla Firefox") {
var keyGet = evtGet.keyCode;
} else {
var keyGet = evtGet.which; //(window.Event) ? evtGet.which : evtGet.keyCode;
}
alert(keyGet);
if ((keyGet > 47 && keyGet < 58) || (keyGet > 64 && keyGet < 91) || (keyGet > 96 && keyGet < 123) || (keyGet == 9) || (keyGet == 32) || (keyGet == 8) || (keyGet == 0) || (keyGet == 13)||(keyGet==110))
return;
else
if (browser == "Microsoft Internet Explorer" || browser == "Mozilla Firefox")
window.event.returnValue = null;
else
evtGet.preventDefault();
}
put the above code in the header of the jsp
and call from input field like
onKeyPress="return isAlphaNumeric(event,this.value);">
suppose you want to change in the code as per your requirement,there is a alert box please run the code 1st then click on the key board it will show keycode as per your requirement
example-you want to enter (.)in the field it will show you keycode for dot id 47 and you can write (keyGet==47)
Related
The below first function code is working absolutely fine for number and dot. However, I want to include comma as well.
This value is stored in an array along with other parameters.
I have also tried the below mentioned 2nd code. It accepts the dot,comma and number only however it stores values as "12.345%2C78" instead of "12.345,78"
function isNumberandComma(evt) {
var iKeyCode = (evt.which) ? evt.which : evt.keyCode
if (iKeyCode != 46 && iKeyCode > 31 && (iKeyCode < 48 || iKeyCode > 57)){
return false;
}
return true;
}
function isNumberandComma(evt) {
const NUM_0 = 48;
const NUM_9 = 57;
const KEY_COMMA = 188;
const KEY_DELETE = 46;
const iKeyCode = evt.which || evt.keyCode;
if (iKeyCode >= NUM_0 || iKeyCode <= NUM_9 || iKeyCode === KEY_COMMA || iKeyCode === KEY_DELETE){
return true;
}
return false;
}
Replace your OR operator (||) with the AND operator (&&).
iKeyCode has to be > 48 AND < 57 OR == 188 OR == 46.
if (iKeyCode >= 48 && iKeyCode <= 57 || iKeyCode === 188 || iKeyCode === 46) {
return true;
}
I need that textbox only allow characters and comma with arrow keys, backspace and delete keys
$(document).ready(function() {
$('#text').keypress(function (e) {
var regex = new RegExp("^[a-zA-Z,]");
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
return true;
}
e.preventDefault();
return false;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<body>
<input type="text" id="text" />
</body>
Check this fiddle
$(document).ready(function() {
$('#text1').keypress(function (e) {
var k = e.which;
var ok = k == 127 || k == 8 || k == 9 || k == 13 || k == 37 || k == 38 || k == 39 || k == 40;
ok = ok ||
k >= 65 && k <= 90 || // A-Z
k >= 97 && k <= 122 || // a-z
k == 44 ; // ,
if (!ok){
e.preventDefault();
}
});
});
I have a textbox, I need to validate my textbox using onkeypress (to restrict special characters). It works fine in Chrome. In Mozilla Firefox it does not fire (Tab Button). Is there any events to add in my code?
My code:
function alpha(e) {
var k;
document.all ? k = e.keyCode : k = e.which;
return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 9); //k=9(keycode for tab)
}
test this code
function alpha(e) {
var code = e.keyCode || e.which;
if(code == 9) { //Tab keycode
//Do something
}
}
This might help you.
function alpha(e){
var k = e.charCode ? e.charCode : e.keyCode;
return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 9); //k=9(keycode for tab)
}
I was trying to validate a string using javascript, I was trying to validate it in a way it couldn't contain any symbol, my code works, but when user inputs any space it will cause an error.
I tried to erase the string but it didn't work.
Here's what I have tried:
function validateSymbol(word)
{
var newword=word.split(' ').join('');
for(var i = 0; i < newword.length; i++)
{
if(word.charCodeAt(i) < 48 ||
newword.charCodeAt(i) > 57 &&
newword.charCodeAt(i) < 65 ||
newword.charCodeAt(i) > 90 &&
newword.charCodeAt(i) < 97 ||
newword.charCodeAt(i) > 122)
{
return false;
}
}
return true;
}
I think you haven't replaced word with newword in first if condition
function validateSymbol(word)
{
var newword=word.split(' ').join('');
for(var i = 0; i < newword.length; i++)
{
if(newword.charCodeAt(i) < 48 ||
newword.charCodeAt(i) > 57 &&
newword.charCodeAt(i) < 65 ||
newword.charCodeAt(i) > 90 &&
newword.charCodeAt(i) < 97 ||
newword.charCodeAt(i) > 122)
{
return false;
}
}
return true;
}
Using keyfilter plugin for jQuery, and all seems to be fine apart from one problem.
I am using a regular expression to filter the element.
$('#nameVal').keyfilter(/[0-9a-zA-Z]/);
The odd thing is, this is not only allowing alpha-numeric characters but it is also allowing '(' to be entered. In fact, it doesn't matter what expression I pass to the keyfilter, I can't stop '(' being allowed in the textbox.
Has anyone else experienced this problem and is there a solution?
I found the problem.
The isSpecialKey function in the KeyFilter plugin returns true if the keycode is 40, which is the keycode for '('. This means that the test is never performed on the character.
var isSpecialKey = function(e)
{
var k = e.keyCode;
var c = e.charCode;
return k == 9 || k == 13 || /*(k == 40 && (!$.browser.opera || !e.shiftKey)) ||*/ k == 27 ||
k == 16 || k == 17 ||
(k >= 18 && k <= 20) ||
($.browser.opera && !e.shiftKey && (k == 8 || (k >= 33 && k <= 35) || (k >= 36 && k <= 39) || (k >= 44 && k <= 45)))
;
};
Commenting out the bit of code as I have above fixes the problem. Not entirely sure what effect this will have in other browsers, but it works in IE8 and Chrome.