Validate Numeric fields

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • somuchbetta
    New Member
    • Mar 2008
    • 5

    Validate Numeric fields

    Hiya,

    Does anyone know any simple JScript code to validate a numeric field and also the length of the data that should be entered into the field.

    e.g. 12345 - the user can only enter numeric fields and the lenght of the value should be five (no more and no less)

    Thanks
  • vee10
    New Member
    • Oct 2006
    • 141

    #2
    Hi,

    u can try out this

    Code:
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>Untitled Page</title>
        <script type="text/javascript">
        function chekc()
        {   
         
        var str = document.getElementById("text1").value;    
        alert(str.length);
        var valid="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        for(i=0;i<str.length;i++)
        {
        if(valid.indexOf(str.charAt(i)) ==-1)
        alert("number");
        else
        alert("not a number");
        }
    
        }
        
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
       <input type="text" id="text1" onchange="chekc()"/>
        </div>
        </form>
    </body>
    </html>

    Originally posted by somuchbetta
    Hiya,

    Does anyone know any simple JScript code to validate a numeric field and also the length of the data that should be entered into the field.

    e.g. 12345 - the user can only enter numeric fields and the lenght of the value should be five (no more and no less)

    Thanks

    Comment

    • somuchbetta
      New Member
      • Mar 2008
      • 5

      #3
      Originally posted by vee10
      Hi,

      u can try out this

      Code:
      <html xmlns="http://www.w3.org/1999/xhtml" >
      <head id="Head1" runat="server">
          <title>Untitled Page</title>
          <script type="text/javascript">
          function chekc()
          {   
           
          var str = document.getElementById("text1").value;    
          alert(str.length);
          var valid="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
          for(i=0;i<str.length;i++)
          {
          if(valid.indexOf(str.charAt(i)) ==-1)
          alert("number");
          else
          alert("not a number");
          }
      
          }
          
          </script>
      </head>
      <body>
          <form id="form1" runat="server">
          <div>
         <input type="text" id="text1" onchange="chekc()"/>
          </div>
          </form>
      </body>
      </html>

      Thanks for the quick reply - the code seems very advanced and im finding it hard to understand it e.g i have no clue what runat="server" means :-( ... and the problem is i need to explain it when im presenting my website.

      At the moment i am only able to check that data has been entered into the field

      [HTML]<script type="text/javascript">
      <!--
      function validate(regfor m) {
      //Perform validation checks

      //Check that telephone number exsit in this field
      if (regform.telno. value=="") {
      alert("Please complete the field: Contact number");
      regform.telno.f ocus();
      return false;
      }
      return true;
      }
      //-->
      </script>


      <form method="get" name="regform" action="regcomp lete.asp" onsubmit="retur n validate(this); ">
      Contact Number:<input type="text" name="telno" size="15" />
      <input type="submit" value="submit">
      </form>[/HTML]
      Last edited by gits; Mar 12 '08, 07:43 AM. Reason: added code tags

      Comment

      • vee10
        New Member
        • Oct 2006
        • 141

        #4
        Hey
        u no need to bother abt the runat ="server" since i am using dot 2.0 i wil get that u just check out the below code if u have any doubt in that code u can contact

        Code:
        function validate(regform)
            {   
             //Check that telephone number exsit in this field
        if (regform.telno.value=="") {
        alert("Please complete the field: Contact number");
        regform.telno.focus();
        return false;
        }
            var str = regform.telno.value;    
            alert(str.length);
            var valid="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
           
            for(i=0;i<str.length;i++)
            {
            if(valid.indexOf(str.charAt(i)) !=-1)    
           
            {
            alert("Please enter numeric");
            return false;
            }
                
            }
            return true;
        
            }


        Originally posted by somuchbetta
        Thanks for the quick reply - the code seems very advanced and im finding it hard to understand it e.g i have no clue what runat="server" means :-( ... and the problem is i need to explain it when im presenting my website.

        At the moment i am only able to check that data has been entered into the field

        <script type="text/javascript">
        <!--
        function validate(regfor m) {
        //Perform validation checks

        //Check that telephone number exsit in this field
        if (regform.telno. value=="") {
        alert("Please complete the field: Contact number");
        regform.telno.f ocus();
        return false;
        }
        return true;
        }
        //-->
        </script>


        <form method="get" name="regform" action="regcomp lete.asp" onsubmit="retur n validate(this); ">
        Contact Number:<input type="text" name="telno" size="15" />
        <input type="submit" value="submit">
        </form>

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5388

          #5
          hi ...

          just a note :) ... there are some shorter ways to accomplish that task:

          [CODE=javascript]// 1. example - uses isNaN-method
          function validate_num_va ls() {
          var val = document.getEle mentById('your_ text_node_id'). value;
          var ret = !isNaN(val);

          if (!ret) {
          alert('enter a number');
          }

          return ret;
          }

          // 2. example - uses regExp
          function validate_num_va ls() {
          var val = document.getEle mentById('your_ text_node_id'). value;
          var ret = /^\d+$/.test(val);

          if (!ret) {
          alert('enter a number');
          }

          return ret;
          }
          [/CODE]
          kind regards

          Comment

          Working...