Reputation: 1097
I want to check validation of special character on key-press or key-up event on a text field which allow space,A-Z,a-z,0-9 only.
any idea thanks
Upvotes: 0
Views: 3605
Reputation: 122956
You can find a demo of this function in this jsfiddle:
function CommonKeyPressIsAlpha(e){
e = e || event;
var matched = (/[a-z]/i).test (String.fromCharCode(e.keyCode || e.which))
document.getElementById('report').innerHTML =
matched ? "" : "Valid input: 'a-z' and/or 'A-Z'"
return matched;
}
To allow digits, change (/[a-z]/i).test
to (/[a-z0-9]/i).test
Upvotes: 2
Reputation: 15960
Following funciton snippet will help you achieve this
Function AlphaNumericValidation(){
var oneChar = "";
for(var i = 0; i < StringSample.length; i++) {
oneChar = StringSample.substring(i, i+1);
if ( (oneChar < "0") || (oneChar > "9" && oneChar < "A") || (oneChar > "Z" && oneChar < "a" ) || (oneChar > "z") )
{
if (isAcceptedChar(oneChar) == false)
{
return false;
}
}
}
return true;
}
Upvotes: 0
Reputation: 7426
Here is a very good writeup on this exact question: http://www.htmlcodetutorial.com/forms/index_famsupp_160.html
Upvotes: 0