Click to See Complete Forum and Search --> : Zip Code validation


rob7765
07-31-2003, 04:18 PM
I am using this simple script to perform validation on my form.

<SCRIPT LANGUAGE=JAVASCRIPT>

//Data validation using object names to reference objects
function validate(form) {
if (form.firstname.value == "") {
alert("You must enter a First Name");
form.firstname.focus()
return false;}


if (form.lastname.value == "") {
alert("You must enter a Last Name");
form.lastname.focus()
return false;}

if (form.email.value == "") {
alert("You must enter a Email Address");
form.email.focus()
return false;}


if (form.zipcode.value == "") {
alert("You must enter a Zip Code");
form.zipcode.focus()
return false;}

if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.email.value)){
return (true)}

alert("Invalid E-mail Address! Please re-enter.")
return (false)}

</SCRIPT>


Can someone tell me how to check to see that the zipcode field is numeric and that it is atleast 5 digits long?

I would like to keep the script very simple like this one so I can understand it. I don't know Javascript at all.

Thanks,

Rob

xataku_nakusute
07-31-2003, 04:22 PM
i believe you could try this:

if (form.zipcode.value == ""|| form.zpicode.value.length < 5) {
alert("You must enter a Zip Code");
form.zipcode.focus()
return false;}

pyro
07-31-2003, 04:22 PM
Try this:

if (!/\d{5,}/.test(form.zipcode.value)) {
alert("You must enter a Zip Code");
form.zipcode.focus()
return false;}