has anyone successfully done JavaScript validation

Discussions

Web tier: servlets, JSP, Web frameworks: has anyone successfully done JavaScript validation

  1. Hi
    Though this is not theclientside.com...i have not been able to get a successful script that checks userinput in a textfield at runtime and only allows numbers upto 2 decimals. Has anyone been able to solve this?

    thanks
    -ram
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT="">
    <META NAME="Keywords" CONTENT="">
    <META NAME="Description" CONTENT="">
    </HEAD>

    <BODY>

    <script language = "Javascript">

    function CheckNumber(){
            var Val=document.frmSample.txtNumber;
            var i;
            var len = Val.value.length;
            var err = "The field should be a number and must be 2 in length";
            if ((Val.value==null)||(Val.value=="") || (len < 2)){
                    alert(err);
                    Val.focus();
                    return false;
            }

        for (i = 0; i < len; i++)
        {
            // Check that current character is number.
            var c = Val.value.charAt(i);
            if (((c < "0") || (c > "9"))) {
    alert(err);
    return false;
    }
        }
        // All characters are numbers.
        return true;

     }
    </script>

    <form name="frmSample" method="post" action="#" onSubmit="return CheckNumber()">
                    Enter a phone number :
                      <input type="text" name="txtNumber" maxlength="2" >
                    
                    
                      <input type="submit" name="Submit" value="Submit">
                    
                  </form>


    </BODY>
    </HTML>


    Thanks,
    Senthil.