You must Sign In to post a response.
  • Category: ASP.NET

    How to validate textbox value using javascript

    Hi all,
    I want below validation for a textbox value using javascript.
    1) length must be 8
    2) It sohuld accept only numeric values.
    How can i do this, please help.
    Thanks,
    Krishna.
  • #671322
    [Response removed by Admin. Read forum policies.]
    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #671340
    hi krishna

    you want to set length 8 then you just add the textbox property <input id="txt" type="text" maxlength="8" />

    you only allow numeric value then use following steps



    <input id="txt" type="text" onkeypress="if(valNumeric(event)==true){return true}else{ return false}"/>



    paste the valnumeric function in javascript page.. the function is


    <script language="javascript" type="text/javascript">
    function valNumeric(evt)
    {
    var charCode;
    charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode >= 48 && charCode <= 57 || charCode== 8 )
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    </script>





    Thanks & Regards

    Saravana Perumal.N

    Thanks & Regards
    --------------------
    Saravana Perumal.N

  • #671360
    Hi Try this code

    Check string is number or not


    function isInteger(s)
    {
    var i;
    for (i = 0; i < s.length; i++)
    {
    // Check that current character is number.
    var c = s.charAt(i);
    if (((c < "0") || (c > "9"))) return false;

    }

    if (s.length < 8) return false;

    // All characters are numbers.
    return true;
    }

  • #671362
    <script type="text/javascript" language="javascript">

    function ValidateNumber(e, strVal)
    {
    var key;
    var keychar;
    if (window.event)
    key = window.event.keyCode;
    else if (e)
    key = e.which;
    else
    return true;

    keychar = String.fromCharCode(key);
    keychar = keychar.toLowerCase();

    // control keys
    if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27)) {

    return false;
    }
    // numbers
    else if ((("0123456789").indexOf(keychar) > -1)) {

    return true;
    }
    else {

    return false;
    }
    }

    function EmptyValidate()
    {

    var tempVal=document.getElementById("txtSample").value;

    if(tempVal=="")
    {
    alert('enter data.....');
    document.getElementById("txtSample").focus();
    return false;
    }

    else if(tempVal<8)
    {
    alert('the length should not less than 8 digits..');
    document.getElementById("txtSample").focus();
    return false;
    }

    }


    </script>


    <asp:TextBox ID="txtSample" runat="server" onkeypress="javascript:return ValidateNumber(event,this.value);">
    </asp:TextBox>

    <asp:Button ID="btnSample" runat="server" Text="Click" OnClientClick="return EmptyValidate()">


    ValidateNumber function accepts only digits.



    Regards,
    Sunitha

  • #671397
    Hi,

    Check out the following code snippet. It insures that the maximum length would not exceed 8 and the textbox would accept only numeric value on textbox's "onkeypress" event.
    It also checks that the value in the textbox must be equal to 8 (not less that) on button click event. If it is, it raised an alert window for notification, if OK, you can proceed to handle button click event in code behind.
    Here you go:


    <script language="javascript" type="text/javascript">
    function chkNumMaxLen(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
    return false;

    return true;
    }

    function CharCounter(textId) {
    var totalchar = 8;
    var txtbox = document.getElementById(textId);
    if (txtbox.value.length > totalchar) {
    txtbox.value = txtbox.value.substring(0, totalchar);
    }
    }

    function FinalCount() {
    var totalchar = 8;
    var txtbox = document.getElementById('<% =TextBox2.ClientID %>');
    if (txtbox.value.length < totalchar) {
    alert("Length must be 8");
    return false;
    }
    else {
    return true;
    }
    }
    </script>

    <div>
    <asp:TextBox ID="TextBox2" MaxLength="8" runat="server" onkeyup="javascript:CharCounter(this.id);"
    onblur="javascript:CharCounter(this.id);" onkeypress="return chkNumMaxLen(event)"></asp:TextBox>
    <asp:Button ID="btnCheckLength" runat="server" Text="Submit"
    onclick="btnCheckLength_Click" />
    </div>


    In the code behind, add the following on page load:


    protected void Page_Load(object sender, EventArgs e)
    {
    btnCheckLength.Attributes.Add("onclick", "javascript:return FinalCount();");
    }


    Hope it'll help you.
    Regards
    Ajatshatru

  • #671407
    try like this code in javascript


    funtion NumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 47 && charCode < 58 || charCode == 127 || charCode == 8) {
    var le=document.getElementById("TextBox1").value.length;
    if(le>8)
    {
    alert('length must be less than 8');
    }
    return true;
    }
    else {
    return false;
    }
    }


    and call that javascript method like below

    <asp:TextBox ID="TextBox1" runat="server" onkeyPress="return NumberKey(event);"></asp:TextBox>


    Further more detail on javascript validation refer myresource
    http://www.dotnetspider.com/resources/43590-How-validate-user-input-using-JavaScript.aspx

    Regards
    N.Ravindran
    Your Hard work never fails

  • #671423

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
    function Enternumericonly()
    {
    var num=window.event.keyCode;

    if(num>=48 & num<=57)
    return;
    window.event.keyCode=0;
    }
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="TextBox1" onkeypress="return Enternumericonly()" MaxLength="8" runat="server"></asp:TextBox>
    </div>
    </form>
    </body>
    </html>




    Thanks & Regards
    G.Renganathan
    Nothing is mine ,Everything is yours!!!

  • #671541

    <head runat="server">

    <script language="javascript" type="text/javascript">
    function AlertAllowNumeric(evt) {
    var keyCode = (evt.which) ? evt.which : event.keyCode
    if (
    (keyCode >= 48 && keyCode <= 57) || // All numerics
    (keyCode == 8) || // Backspace
    (keyCode == 13) // Carriage Return
    ) {
    return true;

    }
    else {
    alert("chars won't allow in this textbox");
    return false;

    }
    }

    function restrictlength() {

    if (document.getElementById("TextBox1").value.length != 8) {
    alert("textbox data length must be eight digits");
    document.getElementById("TextBox1").select();
    document.getElementById("TextBox1").focus();
    return false;
    }
    else {
    return true;
    }
    }


    </script>

    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server" onblur="restrictlength();" onkeypress="return AlertAllowNumeric(event);"></asp:TextBox>
    </form>
    </body>
    </html>


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.