How to check if a string contains special charachter in javascript
Sometimes we need to check if a string contains any special charachter. Ex. name should not contains any special character.
Here is the codesnippet for the same.
function IsSpecialChar(strString)
// check for valid SpecialChar strings
{
var strValidChars = "<>@!#$%^&*()_+[]{}?:;|'\"\\,./~`-=";
var strChar;
var blnResult = true;
if (strString.length == 0) return false;
// test strString consists of valid characters listed above
for (i = 0; i < strString.length && blnResult == true; i++) {
strChar = strString.charAt(i);
if (strValidChars.indexOf(strChar) == -1) {
blnResult = false;
}
}
return blnResult;
}
Enjoy