Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...This is a very good site. Please keep it running. Thanks and wishing a great health and success for the site and its owners..."

Geography

Where in the world do Tek-Tips members come from?

Validating forms with an exception

angela (Programmer)
13 May 00 16:44
I have spent so much time on it that I've got myself confused!  I am trying to make a form with and address. (city, state, and zip) where it will validate it.  The city must be greater than 2 characters long, with no numbers, the state must be 2 characters long with no numbers.  The zip, either 5 or 10 characters long.  If any of these are wrong, I need an alert to come up.  This I can do fine.  

Here's the problem:  If zip is correct, the city and state can be blank.  If the city and state are not blank, they need to be varified by the above explaination.

How can I code this?  I am a fairly new JavaScript user and my brain is fried!

Thanks!
palbano (Programmer)
14 May 00 2:29
Dear angela,

I tested this is IE 4 and 5 and nutscrap 4.06.

Hope this helps
-pete

function form1_onsubmit() {

var sCity = document.form1.city.value;
var sState = document.form1.state.value;
var sZip = document.form1.zip.value;

// City, State, Zip valid flags
var validFlags = new Array(false,false,false);

// matches 5 digits
var re = new RegExp("^([0-9]{5})$");

// see if zip matches 're'
validFlags[2] = ( 0 == sZip.search(re))


// if zip is not valid check '##### ####'
if ( !validFlags[2]){
// matches 5 digits space 4 digits
re = new RegExp("^([0-9]{5} [0-9]{4})$");
validFlags[2] = ( 0 == sZip.search(re));
}

if ( validFlags[2]){
        // city and state can be empty
if ( 0 == sCity.length)
validFlags[0] = true;
if ( 0 == sState.length)
validFlags[1] = true;
}

if ( !validFlags[0]){ // City

re = new RegExp("^([a-zA-Z]{3,})$");
validFlags[0] = ( 0 == sCity.search(re));
}

if ( !validFlags[1] ){ // State

re = new RegExp("^([a-zA-Z]{2})$");
validFlags[1] = (0 == sState.search(re));
}

if (!validFlags[0] ¦¦ !validFlags[1] ¦¦ !validFlags[2])
alert("not valid");

return validFlags[0] &&
validFlags[1] &&
validFlags[2];
}

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close