...

View Full Version : zipcode edit field



petertran123
08-02-2002, 03:09 PM
Hello all Helper,

Im using the code below to validate the zipcode field, but not the way i want it to be. The problem is zipcode field is not a required a field client allow to be able to submit without entering data on zipcode field. In the other hands, if client fill out the zipcode field less than 5 numeric then the validate message pop up say something (invalid zipcode). In my HTML code i set onblur="ValidateUSZip()" but everytime i tabbed over the field and received the message popup. I wanted the field to validate when client edit the field, if they are not edit the parameter will pass through. Please help to solve this problem. I'd appreciate any help.

Thanks,
Peter.

<script language="Javascript">
function ValidateUSZip() {
var re=/\d{5}/;
if(!re.test(document.forms[0].PrimaryZip.value))
alert("The zip code field must be five digits long. (ex: 40202)");
}
</script>

kansel
08-02-2002, 03:28 PM
Check to see if the field is blank first. If it is blank you don't need to process it.

<script language="Javascript">
function ValidateUSZip() {
if(document.forms[0].PrimaryZip.value != ""){
var re=/\d{5}/;
if(!re.test(document.forms[0].PrimaryZip.value))
alert("The zip code field must be five digits long. (ex: 40202)");
}
}
</script>

petertran123
08-02-2002, 03:58 PM
Thanks for your help Kansel

petertran123
08-05-2002, 04:06 PM
The problem i got when tesing the application. Every time i edited the field and it won't let me tab over to next field. Do you know why, please help me..that is a only problem i have right now.

Thanks again,

kansel
08-05-2002, 10:24 PM
With the script from my last post and using the following test form, this seems to work correcly.

<form>
<input>
<input name="PrimaryZip" onblur="ValidateUSZip()">
<input>
</form>

I can tab over the field without putting any info in and no alert box. If I put data in, it has to contain a 5-digit number in order to pass the test. If I put in less than 5 digits it alert()s.

If you're having problems with this, could you post the complete form code or a link to the complete code for me to debug?

petertran123
08-06-2002, 02:17 PM
This is a complete code and it won't let me tab over to next field

<script language="Javascript">
function ValidateUSZip() {
if(document.forms[0].PrimaryZip.value != ""){
var re=/\d{5}/;
if(!re.test(document.forms[0].PrimaryZip.value))
alert("The zip code field must be five digits long. (ex: 40202)");
document.form.PrimaryZip.focus();
return false;
}
}
</script>


the reason i added document.form.PrimaryZip.focus() that i wanted the cursor will be redirected to the field, but the problem i got it is won't let me tab over to next field even i edit with 5 digits numeric. Please help me to debug it.

Thanks,
Peter

kansel
08-06-2002, 03:45 PM
You only want the field to refocus if the zip is invalid. The way it is set up now it will refocus whether it is valid or not.

In order to refocus only on invalid zip, you need to enclose that code in {braces}.

if(!re.test(document.forms[0].PrimaryZip.value))
alert("The zip code field must be five digits long. (ex: 40202)");
document.form.PrimaryZip.focus();
return false;

should be changed to

if(!re.test(document.forms[0].PrimaryZip.value))
{alert("The zip code field must be five digits long. (ex: 40202)");
document.forms[0].PrimaryZip.focus();
return false;}

If the test (!re.test(document.forms[0].PrimaryZip.value) passes, the script will execute the following commands:
1) alert("The zip code field must be five digits long. (ex: 40202)")
2) document.forms[0].PrimaryZip.focus()
3) return false

If the test fails (i.e. PrimaryZip is valid) the script will simply exit.

-kansel

petertran123
08-06-2002, 04:06 PM
Thanks Mr. Kansel

It's work great...i totally forgot to add the braces for an alert..

Thanks again for your great help
Peter

beetle
08-06-2002, 04:08 PM
My Validator (http://www.peterbailey.net/jsdemo/) does ZIPs!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum