dan_t Posted January 6, 2009 Share Posted January 6, 2009 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 https://forums.phpfreaks.com/topic/139720-validating-trouble/ Share on other sites More sharing options...
KevinM1 Posted January 6, 2009 Share Posted January 6, 2009 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 https://forums.phpfreaks.com/topic/139720-validating-trouble/#findComment-731037 Share on other sites More sharing options...
Psycho Posted January 7, 2009 Share Posted January 7, 2009 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 https://forums.phpfreaks.com/topic/139720-validating-trouble/#findComment-731245 Share on other sites More sharing options...
dan_t Posted January 7, 2009 Author Share Posted January 7, 2009 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 https://forums.phpfreaks.com/topic/139720-validating-trouble/#findComment-731773 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.