Click here to Skip to main content
16,020,811 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to validate textbox which takes only five digits in it in javascriptshow alert otherwise.
Posted

Use the Regular expression with a java script like below.

Java
function validate()
{
 var ex = /^[0-9]{5}$/;
 if(ex.test(document.getElementById('textBoxId').value) == false)
 {
    // alert code goes here
 }
}



Call this function on onblur evenr of the text box.

Java
<input type="text" id="textBoxId"  onblur="validate();" />


BR//
Harsha
 
Share this answer
 
v3
try this

//By Aspx--------------------------
 
 <asp:TextBox ID="TextBox1" MaxLength="5" runat="server"></asp:TextBox>


// ViaJavascript-------------------------------------

function CheckLength(events, args)
{
 var x = (document.getElementById(txtTestForLength).value);

    if(X.length>5)
    {
        args.IsValid = false;
    }
    else
    {
             if(isNaN( x)
                {
                 args.IsValid = false;
                }
        else
        {
        args.IsValid = true;
         }
}

}
</script
 
Share this answer
 
v2
Comments
ZeeroC00l 10-May-11 6:51am    
Your solution will work for checking maximum 5 characters in the input text box. The question was for validating the text box that takes 5 Digits.

You can make use of isNaN along with length to check for the digits during validation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900