/    Sign up×
Community /Pin to ProfileBookmark

Verify form data: Email OR Address

Hi all,

I can’t seem to figure this one out. Any help is appreciated. I want to verify that the first name and the last name are entered, as well as either the email address OR the mailing address. The following code is as good as I could get from online resources, etc:

[CODE]<!–
function Verification() {

if (document.forms[0].FirstName.value == “” || document.forms[0].LastName.value == “”) {
window.alert (“You must enter your first and last name”);
return false;
}

if (document.forms[0].Email.value == “” || (document.forms[0].Street.value == “”
&& document.forms[0].City.value == “” && document.forms[0].State.value == “”
&& document.forms[0].Zip.value == “”)) {
window.alert (“You must enter either your email or mailing address.”);
return false;
}
if (document.forms[0].Email.value != “” || (document.forms[0].Street.value != “”
&& document.forms[0].City.value != “” && document.forms[0].State.value != “”
&& document.forms[0].Zip.value != “”)) {
return true;
}
}
–>
</script>[/CODE]

This code works for first and last name, but requires that both email and the “street” field be populated. I just want it to basically say, “If the email field OR (ALL) of the Address fields are populated, then return true.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@howejustinauthorJun 23.2009 — Anybody...beuler...beuler?
Copy linkTweet thisAlerts:
@animalj7Jun 23.2009 — Your second if criteria is wrong.

It translates to if email is blank or if all of the address fields are blank. It looks like you want it to be if email is blank or if any of the address fields is blank.

Something like this...
[CODE]email == "" || (street == "" || city == "" || ...[/CODE]

However, you can remove the 2nd If completely and add an else to the 3rd If to cover that criteria.

The 3rd translates to if email is not blank or all of the address fields are not blank return true. Your validation is in the opposite case.

Something like this...
[CODE]
if (document.forms[0].Email.value != "" || (document.forms[0].Street.value != "" && document.forms[0].City.value != "" && document.forms[0].State.value != "" && document.forms[0].Zip.value != ""))
{
return true;
}
else
{
window.alert ("You must enter either your email or mailing address.");
return false;
}
[/CODE]
Copy linkTweet thisAlerts:
@howejustinauthorJun 23.2009 — It's so easy! I knew it was something like that. I was really tired and couldn't realize what I was forgetting to do.

*smacks forehead*

Thanks man.
×

Success!

Help @howejustin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...