Jump to content

validating trouble


dan_t

Recommended Posts

Hello,

Could someone take a look at tis and see where I went wrong?

I tried validating it, it did not show anything that made a difference.

Thanks

Dan

 

<script language="javascript" type="text/javascript">

<!-- begin

function validateZip(field) {

var  valid = "0123456789-";

var hyphencount = 0;

if (field.lenght!=5 && field.lenght!=10) {

alert("Please enter your five digit or five digit +4 zip code");

return false;

}

 

for (var i=0; i < field.lenght; i++) {

temp = "" + field.substring(i, i+1);

if (temp == "-") hyphencount++;

if (valid.indexOf(temp) == "-1") {

alert("Invalid characters in your zip code. Please try again.");

return false;

}

 

if ((hyphencount > 1) || ((field.lenght == 10) && ""+field.charAt(5)!="-")) {

alert("The hyphen character should be used with a properly formatted 5 digit+4 zip code like '12345-6789'. Please try again.");

return false;

}

}

return true;

}

//end-->

</script>

</head>

 

<body>

<form name="zip" onsubmit="return validateZIP(this.zip.value)">

Zip: <input type="text" size="30" name="zip" />

<input type="submit" value="submit" />

</form>

Link to comment
Share on other sites

That's a bit of a long-winded approach to doing something as easy as checking a zipcode.  Ideally, you should use regular expressions when validating fields like these.  What are regular expressions?  They're essentially expressions that represent a pattern that you want to test a value against.  In this case, a 5-digit code, potentially followed by a hyphen and a 4-digit code.

 

With regEx, this is simple to do.  Even better, JavaScript has built-in regEx functionality.

 

Your script should look like:

 

<script type="text/javascript">
   window.onload = function() // we do this to ensure the HTML is all loaded before the script
   {

      /* put the rest of your validation code in here, too */

      function validateZip(field)
      {
         var zip = field.value;
         var regEx = /^\d{5}(-\d{4})?$/;

         if(!zip.match(regEx))
         {
            alert("The zipcode you entered is wrong.  It must contain 5 digits, with an optional hyphenated 4 digit component (ex: 12345-1234)");
            return false;
         }
         else
         {
            return true;
         }
      }
   }
</script>

Link to comment
Share on other sites

I agree with Nightslyr, but to answer your question as to why your original code didn't work, I suspect it is because of this line:

temp = "" + field.substring(i, i+1);

 

That will actually grab TWO characters and not ONE such as you are testing against.

Link to comment
Share on other sites

Actually, I found two things looking it over (a half-a-million times).

1. I spelled length wrong.

2. The non-capital Zip on the function.

 

I am sure there are many more.

I am not up to speed on javascript yet, so I haven't learned a lot about regex yet.

Thanks

Dan

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.