Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-06-2012, 01:04 PM   PM User | #1
nicknick
New to the CF scene

 
Join Date: Apr 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
nicknick is an unknown quantity at this point
Problem in zip code validation

Hello! There is something wrong in the code below. What is it? Can you help me please? The form validation was working just fine when I added the last 'if' for zip code validation. The function didn't work and returned true. The code is:

Code:
<script type="text/javascript">
function validateForm()
{
var y=document.forms["form"]["firstname"].value;
var f=document.forms["form"]["zipcode"].value;
var x=document.forms["form"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (y==null || y=="NAME")
  {
  alert("First name must be filled out");
  return false;
  }
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  alert("Not a valid e-mail address");
  return false;
  }
}
var re='/(^\d{5}$)/';
if (f!=re)
  { 
  alert ("Not a valid address");
  return false;
  }
}
</script>
Any ideas or advice? Thank you in advance!
nicknick is offline   Reply With Quote
Old 04-06-2012, 01:46 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,100
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Code:
if (!/\d{5}/.test(f)) {
alert ("Invalid zip code");
// erase the field with wrong info in it
// and refocus on it
return false;
}

You may wish to use
if (/^\d{5}\-?\d{4}$/gi.test(f)) { // 5 digits, optional hyphen, 4 digits
as I understand some US zip codes are in the form 12345-6789


Quizmaster: In 1918 the Czechs and the Slovaks united to form which country?
Contestant: Prussia
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
nicknick (04-06-2012)
Old 04-06-2012, 02:02 PM   PM User | #3
nicknick
New to the CF scene

 
Join Date: Apr 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
nicknick is an unknown quantity at this point
Thank you very much Philip! Now it is working! Thank you again!

Last edited by nicknick; 04-06-2012 at 02:12 PM..
nicknick is offline   Reply With Quote
Reply

Bookmarks

Tags
code, validation, zip

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:07 AM.


Advertisement
Log in to turn off these ads.