View Full Version : Javascript - zipcode verification
mysterio2099
07-22-2005, 11:42 AM
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:
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.
:eek: THANKS :eek:
Stryker
07-22-2005, 12:06 PM
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.
doctorgonzo
07-22-2005, 12:14 PM
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.
mysterio2099
07-22-2005, 12:15 PM
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
07-22-2005, 12:16 PM
way over my head Doc, lol
doctorgonzo
07-22-2005, 12:31 PM
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.
mysterio2099
07-22-2005, 12:31 PM
well, I was guessing that, but I don't actually understand the entire code, what does what...
Stryker
07-22-2005, 12:33 PM
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.
function IsNumeric(sText)
{
var ValidChars = "0123456789.";
var IsNumber=true;
var Char;
for (i = 0; i < sText.length && IsNumber == true; i++)
{
Char = sText.charAt(i);
if (ValidChars.indexOf(Char) == -1)
{
IsNumber = false;
}
}
return IsNumber;
}
mysterio2099
07-22-2005, 12:58 PM
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.
vBulletin® v3.7.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.