Validate Numbers and also Mimimum and Maximum Length of characters for TextBox using JavaScript

Last Reply on Oct 22, 2012 09:13 AM By Mudassar

Posted on Oct 22, 2012 09:11 AM

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:

<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 on Oct 22, 2012 09:13 AM Modified on on Oct 22, 2012 09:15 AM

This way

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

            if (Tel.value == "") {
                alert("Enter Your Telephone No");
                Tel.focus();
                return false;
            }
            debugger;
            var numPat = /[^0-9]/;
            if (numPat.test(Tel.value)) {
                alert("Telephone No must contain only digits");
                Tel.focus();
                return false;
            }
            Tel.value = parseInt(Tel.value);
            var emailPat = /^[\s\S]{10,11}$/;
            var emailid = Tel.value;
            var matchArray = emailPat.test(emailid);
            if (matchArray == false) {
                alert("It allows only digits (10-11 digits)");
                Tel.focus();
                return false;
            }
        }
    </script>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick = "return validate()" />

 

Demo