Click here to Skip to main content
15,919,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have to do numbers validation using javascript, In that i have few douts about javascript, My application flow should like this,
i have one textbox which i validate to allow only digits , in digits i have allow up to 10,11 digits only but in my javascript code which cannot allow characters which is allowed only numbers thats i need But its cannot allow upto 10,11 digits, please find out the below code,

javascript:

XML
<script type="text/javascript">
    function validate()
    {
    var Tel = document.getElementById("<%=TextBox1.ClientID%>")

    if(Tel.value == "")
    {
    alert("Enter Your Telephone No");
    Tel.focus();
    return false;
    }
    var emailPat = /d[10,11]+$/;
    var emailid=Tel.value;
    var matchArray = emailid.match(emailPat);
    if (matchArray == null)
    {
    alert("It allows only digits (10-11 digits)");
    Tel.focus();
    return false;
    }


    }
    </script>
Posted

Hay.... you can try this one

JavaScript
function PhoneValidations(event) {
    var val;
    if (navigator.appName == "Microsoft Internet Explorer")
        val = window.event.keyCode;
    else
        val = event.which;
    if (val == 0)
        val = event.keyCode;

    if ((val >= 48 && val <= 57) || val == 45 || val == 37 || val == 39 || val == 9 || val == 40 || val == 41 || val == 32) {
        return true;
    }
    else if (val == 8)
        return true;
    else
        return false;
}


In Args you need to pass the e. (i.e. the event of page)

Umesh Pansara
 
Share this answer
 
v2
If you are using C# then try this.
This simple then javascript

XML
<asp:TextBox ID="txt_ID" runat="server" ></asp:TextBox>
//Add Extender
<asp:FilteredTextBoxExtender ID="txt_ID_FilteredTextBoxExtender" FilterType="Numbers" runat="server" Enabled="True" TargetControlID="txt_ID"> </asp:FilteredTextBoxExtender>
 
Share this answer
 
v2
Comments
stellus 22-Oct-12 7:57am    
Hi arun thanks for yr comments, its working fine in c# but i dont want use server side,i just wanted to use only client side using javascript, if you have any ideas or else something code regarding javascript based on this concept then please let me know its very urjent.

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