SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2005
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Number range validation has problem

    suppose only number between 1 to 100 can be accepted. but, when i tried inserting number more than 100, nothing happened. no error message is displayed.

    Code:
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="javascript">
    function validate(string) {
    	var newString = parseInt(string.value);
        if (!string) 
    	{	
    		document.all.attendanceSeatNo.value = '';
    		document.all.attendanceSeatNo.focus();
    		return false;
    	}
    	
    	else if (newString > 0 || newString > 100)	
    	{
    		document.all.attendanceSeatNo.value = '';
    		document.all.attendanceSeatNo.focus();
    		return false;
    	}
    	else
    	{
        	var Chars = "0123456789";
    	
        	for (var i = 0; i < string.length; i++) {
           	if (Chars.indexOf(string.charAt(i)) == -1)
              	return false;
        	}
        	return true;
    	}
    } 
    </script>
    </head>
    
    <body>
    <form name="form1" method="post" action="">
      <table width="200" border="1">
        <tr>
          <td><input name="attendanceSeatNo" type="text" id="attendanceSeatNo" onChange="if (!validate(this.value)) alert('Invalid Seat No.! \n Seat No. must be numbers and within range 1 to 100.')"></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td><input type="submit" name="Submit" value="Submit"></td>
        </tr>
      </table>
    </form>
    </body>
    </html>

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if(string.length == 0 || newString > 100) return false
    cheers

  3. #3
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wackyflik
    Code:
    document.all.attendanceSeatNo.value = '';
    Never use document.all to refer to form elements. Use: document.forms.formName.elementName

    The onchange event will not fire if the field is left blank.

    Validation should be triggered via the onsubmit event.

    Code:
    function validate(string) {
    	var newString = parseInt(string.value);
    The parameter being passed to the function is a string, which has no .value property, therefore newString will be NaN -

    Code:
    (newString > 0 || newString > 100)
    therefore the test above (which in any case is wrong), always fails.
    Code:
    function validate(string)
    {
     var ok=true;  
     
     string=string.replace(/\s/g,'')-0;
     
     if( string=="" || /[^0-9]/.test(string) || string < 1 || string > 100) 
     {
      document.forms.form1.attendanceSeatNo.value = '';
      document.forms.form1.attendanceSeatNo.focus();
      ok=false;
     }
    
     return ok;
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •