Friday, November 19, 2010

How to check a given string is a Valid Alphabet, Valid number and Valid alpha numeric in javascript

 Check a given string a Valid Alphabet, Valid number and Valid alpha numeric in javascript


// Checks whether the string contains only alphabets
_this = String.prototype;
_this.isValidAlphabet =
function()
{
var
charpos = this.search("[^A-Za-z]");
if(this.length > 0 && charpos >= 0)
return false;
return true;
}
;
// Checks whether the string is valid number
_this.isValidNumber =
function()
{
var
charpos = this.search("[^0-9]");
if(this.length > 0 && charpos >= 0)
return false;
return true;
}
;
// Checks whether the string is a valid alpha numeric value
_this.isValidAlphaNumeric =
function()
{
var
charpos = this.search("[^A-Za-z0-9-_ ]");
if(this.length > 0 && charpos >= 0)
return false;
return true;
}

...

Usage  :

var str = document.forms[0].element['name'].value;

alert(str.isValidAlphabat())
....
///

No comments:

Post a Comment