Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 07-22-2005, 10:42 AM   #1
Banned
 
mysterio2099's Avatar
 
Join Date: Aug 2003
Posts: 349
Send a message via AIM to mysterio2099 Send a message via MSN to mysterio2099 Send a message via Yahoo to mysterio2099
Javascript - zipcode verification

I have a simple HTML based form that uses Javascript to verify that the fields are entered, BUT, I need the zipcode to be verified as only numbers. I don't want people putting letters in, ya know?

Here is the code I'm using for the other fields:

Code:
function validate(form) { 

    if (form.firstname.value.length == 0)
    { 
        alert("Please enter your first name.") 
        form.firstname.focus() 
        return false 
    }

with that said, how can I validate the zipcode field?....I'm a n00b at coding, so, hit me hard, but hit me easy.

THANKS
mysterio2099 is offline   Reply With Quote
Old 07-22-2005, 11:06 AM   #2
It can never be too quiet
 
Stryker's Avatar
 
Join Date: May 2004
Location: Burlington, Ontario
Posts: 1,090
Hiya mysterio,
This should do what you need:
http://www.pbdr.com/vbtips/asp/JavaNumberValid.htm

If you don't need the second part of the script, you can just rip it out and use only the numeric check.
__________________
Athlon XP 2800+ • Asus A7N8X (nVidia Nforce2) • Radeon 9600 Pro 256MB • 2x512MB KVR DDR PC3200 Dual Channel • 120GB Seagate 7200RPM 8MB • 160GB WD 7200RPM 8MB • Liteon DVD±RW DL • AOpen DVD±RW DL • Vantec Ion2 350W PSUSend me your picture for the Member Photo Gallery
Stryker is offline   Reply With Quote
Old 07-22-2005, 11:14 AM   #3
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
I don't know the exact functions in Javascript, or even if they exist, but when I do this in other languages, what I do is cast the text to a number, then back to text, and compare it to the original data entered. If it is all numbers, it should match; if not, then it won't.

For example, if somebody enters "123N" in the ZIP code, casting that to a number (using CVal in Visual Basic, for example) changes that to 123, and when you compare that to "123N", they are different. If a pure number is entered, though, they will match.
doctorgonzo is offline   Reply With Quote
Old 07-22-2005, 11:15 AM   #4
Banned
 
mysterio2099's Avatar
 
Join Date: Aug 2003
Posts: 349
Send a message via AIM to mysterio2099 Send a message via MSN to mysterio2099 Send a message via Yahoo to mysterio2099
AAAAAlllrighty...to make it simple, that code is way over my head, and needs explanation. I hate using code that I don't understand.

Could you ....possibly explain it? The part that I need anyhow.

Thanks Stryker, this will be the 2nd time you've helped me that I can remember.
mysterio2099 is offline   Reply With Quote
Old 07-22-2005, 11:16 AM   #5
Banned
 
mysterio2099's Avatar
 
Join Date: Aug 2003
Posts: 349
Send a message via AIM to mysterio2099 Send a message via MSN to mysterio2099 Send a message via Yahoo to mysterio2099
way over my head Doc, lol
mysterio2099 is offline   Reply With Quote
Old 07-22-2005, 11:31 AM   #6
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
Basically, the code Stryker links to checks to make sure that each character in the Test string is in the Validation string (var strValidChars = "0123456789.-";). Thus, if any characters in the test string are not in that Validation string, the function returns false.
doctorgonzo is offline   Reply With Quote
Old 07-22-2005, 11:31 AM   #7
Banned
 
mysterio2099's Avatar
 
Join Date: Aug 2003
Posts: 349
Send a message via AIM to mysterio2099 Send a message via MSN to mysterio2099 Send a message via Yahoo to mysterio2099
well, I was guessing that, but I don't actually understand the entire code, what does what...
mysterio2099 is offline   Reply With Quote
Old 07-22-2005, 11:33 AM   #8
It can never be too quiet
 
Stryker's Avatar
 
Join Date: May 2004
Location: Burlington, Ontario
Posts: 1,090
Ok... here it goes:

validChars - this is the list of characters that will be acceptable as valid. If it's a ZIP, you can probably remove the "." from that variable and just check for numbers.

The "for" statement is checking (counting through) each character in the field content that was passed using "sText". It checks each character to make sure it matches one of the characters in "validChars" (i starts at 0 and counts each character until it reaches the end of sText.length, which is the last character), and if something doesn't match it sets "IsNumber" to false.

Once all is said and done, use IsNumber to give an alert or stop submission (i.e. if IsNumber is false, alert Invalid ZIP entered.).

Hopefully that was helpful, but if you need more info on individual parts of the script, www.w3schools.com is a great resource. I'm not trying to get rid of you, just that way you don't have to wait for me to try and fumble through to explain it.


PHP Code:
function IsNumeric(sText)

{
   var 
ValidChars "0123456789.";
   var 
IsNumber=true;
   var 
Char;
 
   for (
0sText.length && IsNumber == truei++) 
      { 
      
Char sText.charAt(i); 
      if (
ValidChars.indexOf(Char) == -1
         {
         
IsNumber false;
         }
      }
   return 
IsNumber;
   
   } 
Stryker is offline   Reply With Quote
Old 07-22-2005, 11:58 AM   #9
Banned
 
mysterio2099's Avatar
 
Join Date: Aug 2003
Posts: 349
Send a message via AIM to mysterio2099 Send a message via MSN to mysterio2099 Send a message via Yahoo to mysterio2099
Alright, thanks Stryker, I'll look into it. You helped explain part, but not all. Can't ask for anything more (unless you would like to explain more, lol). Btw, check your PM messages...I sent something regarding Folding@Home.
mysterio2099 is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes 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 On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 04:54 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2