Thursday, December 4, 2008

Javascript to validate textbox with alphanumeric

Hi,

Need:Textbox should contain only AlphaNumeric.
I want to validate whether the textbox is empty or contains any special characters.If so, i want to generate proper error message .We can do validation in javascript by using Regular Expression.
Here is the code,
function ValidateTextBox()
{
document.getElementById("snameSpan").innerHTML="";
var s1=document.getElementById("snameTextBox").value;
var re=new RegExp("^[a-zA-Z,0-9]+$");
if(s1=="")
{
document.getElementById("snameSpan").innerHTML="Enter your name";
}
else if(s1 != "")
{
if(!re.test(s1))
{
document.getElementById("snameSpan").innerHTML="Invalid name";
}
}
}

No comments: