|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Banned
|
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
|
|
|
|
|
|
#2 |
|
It can never be too quiet
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 |
|
|
|
|
|
#3 |
|
Professional gadfly
|
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. |
|
|
|
|
|
#4 |
|
Banned
|
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. |
|
|
|
|
|
#5 |
|
Banned
|
way over my head Doc, lol
|
|
|
|
|
|
#6 |
|
Professional gadfly
|
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.
|
|
|
|
|
|
#7 |
|
Banned
|
well, I was guessing that, but I don't actually understand the entire code, what does what...
|
|
|
|
|
|
#8 |
|
It can never be too quiet
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:
|
|
|
|
|
|
#9 |
|
Banned
|
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.
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|