Wednesday, June 15, 2011

Javascript function to check for valid characters and invalid characters

If you want to validate a name feild with only alphabets. you can use below code to validate it.

<script  type="text/javascript"> 
if  (IsValidChars(document.getElementById( ).value.toLowerCase(), "abcdefghijklmnopqrstuvwxyz" )) {
alert("valid" );
}
else  {
alert("Invalid Characters" );
}
//Function to check the valid characters 
function  IsValidChars(Char_To_Be_Checked, Valid_Char) {
var  checkStr;
var  checkOK1;
var  allValid;
checkStr = Char_To_Be_Checked;
checkOK1 = Valid_Char;
var  allValid = true ;
for  (m = 0; m < checkStr.length; m++) {
ch = checkStr.charAt(m);
for  (n = 0; n < checkOK1.length; n++) {
if  (ch == checkOK1.charAt(n))
break ;
}
if  (n == checkOK1.length) {
allValid = false ;
break ;
}
}
return  allValid;
}
//Function to check the invalid characters 
function  IsInValidChars(Char_To_Be_Checked, InValid_Char) {
var  checkStr;
var  checkOK1;
var  allValid;
checkStr = Char_To_Be_Checked;
checkOK1 = InValid_Char;
var  allValid = true ;
for  (m = 0; m < checkStr.length; m++) {
ch = checkStr.charAt(m);
for  (n = 0; n < checkOK1.length; n++) {
if  (ch == checkOK1.charAt(n)) {
allValid = false ;
break ;
}
}
}
return  allValid;
}	
</script>


No comments:

Post a Comment