SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast Dontom's Avatar
    Join Date
    Mar 2001
    Location
    Germany
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Validate Number with leading zero as number

    HI ,
    I use this script to validate the entry of a zip-code (minimum 4, maximum 5 characters)
    Unfortunately in Germany we have zips starting with a 0, in this case the Javascript pops up and asks for a correct number. How can I avoid this?
    Thank you,
    Tom


    Code:
    <script type="text/javascript" language="javascript">
    function validateNumber(field, msg, min, max) {if (!min) { min = 0 }if (!max) { max = 255 }if ( (parseInt(field.value) != field.value) ||         field.value.length < min || field.value.length > max) {alert(msg);field.focus();field.select();return false;}return true;}
    </script>
    ...
    <form name="eingabe" action="fgeintragen2.php" method="post"  onsubmit="return (
    validateNumber(this.plz, 'Please enter a valid zip-code', 4, 5)
    );" >

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    Re: Validate Number with leading zero as number

    Originally posted by Dontom
    HI ,
    I use this script to validate the entry of a zip-code (minimum 4, maximum 5 characters)
    Unfortunately in Germany we have zips starting with a 0, in this case the Javascript pops up and asks for a correct number. How can I avoid this?
    Thank you,
    Tom


    Code:
    <script type="text/javascript" language="javascript">
    function validateNumber(field, msg, min, max) 
    {
    if (!min) { 
    min = 0 
    }
    if (!max) 
    { 
    max = 255 
    }
    
    if ( (parseInt(field.value) != field.value) || field.value.length < min || field.value.length > max) 
    {
    alert(msg);
    field.focus();
    field.select();
    return false;
    }
    return true;
    }
    </script>
    ...
    <form name="eingabe" action="fgeintragen2.php" method="post"  
    onsubmit="return (validateNumber(this.plz, 
    'Please enter a valid zip-code', 4, 5));" >
    Here's a much faster script using regular expressions:

    Code:
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="javascript" type="text/javascript">
    function validateNumber (field) {
      var RE = new RegExp(/^(\d){4}(\b|\d\b)/);
      var result = RE.exec(field.value);
      if (!result) {
      alert ('Please enter a valid zip code!')
      }
      return result;
    }
    </script>
    
    </head>
    
    <body>
    <form name="form1" action="#" method="post" >
    	<input type="text" name="text1" size="5" />
    	<input type="button" name="submit" value="validate" onClick="return validateNumber(this.form.text1);"/>
    </form>
    </body>
    </html>
    This regular expression makes sure there are only 4 or 5 digits in the text box, no more no less. Hope this helps you out!

    --Vinnie
    Last edited by vgarcia; Oct 25, 2002 at 05:49.

  3. #3
    SitePoint Enthusiast Dontom's Avatar
    Join Date
    Mar 2001
    Location
    Germany
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, i will try this!
    Tom

  4. #4
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    Re: Re: Validate Number with leading zero as number

    Here's one that ensures there's a "0" in the first digit:

    Code:
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="javascript" type="text/javascript">
    function validateNumber (field) {
      var RE = new RegExp(/^0(\d){3}(\b|\d\b)/);
      var result = RE.exec(field.value);
      if (!result) {
      alert ('Please enter a valid zip code!')
      }
      return result;
    }
    </script>
    
    </head>
    
    <body>
    <form name="form1" action="#" method="post" >
    	<input type="text" name="text1" size="5" />
    	<input type="button" name="submit" value="validate" onClick="return validateNumber(this.form.text1);"/>
    </form>
    </body>
    </html>


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
  •