/    Sign up×
Community /Pin to ProfileBookmark

Field + Email validation

Hey ..

I have a script that validates the fields and a check box that I need ..

But I also want it to make sure a valid email address is submited ..

I have been trying for ages to merge it into my script .. with no luck ..

I thought a javascript guru maybe able to help me out ..

Here is my script:

[code=php]

<SCRIPT LANGUAGE=”JavaScript”>

function validateForm()
{
formObj = document.cart;
if (formObj.name.value == “”)
{
alert(“You have not entered your name.”);
formObj.name.focus();
return false;
}
else if (formObj.email.value == “”)
{
alert(“You have not entered your email address.”);
formObj.email.focus();
return false;
}
else if (formObj.phone.value == “”)
{
alert(“You have not entered a contact phone number.”);
formObj.phone.focus();
return false;
}
else if (formObj.address.value == “”)
{
alert(“You have not entered your shipping address.”);
formObj.address.focus();
return false;
}
else if (formObj.city.value == “”)
{
alert(“You have not entered the city you live in.”);
formObj.city.focus();
return false;
}
else if (formObj.terms.checked == false)
{
alert(“You have not agreed to our terms and conditions.”);
formObj.terms.focus();
return false;
}
}

</SCRIPT>

[/code]

Cheers

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@javaNoobieOct 14.2004 — <i>
</i>else if (formObj.email.value == "" || (!/^(w[-._w]*w@w[-._w]*w.w{2,3})$/.test(formObj.email.value))){
alert("You have not entered your email address.");
formObj.email.focus();
return false;
}

Copy linkTweet thisAlerts:
@bob_the_builderauthorOct 14.2004 — Hey javaNoobie,

Thanks for that .. Works great

Cheers
Copy linkTweet thisAlerts:
@Jeff_MottOct 14.2004 — /^(w[-._w]*w@w[-._w]*w.w{2,3})$/[/quote]What about addresses such as [font=courier][email protected][/font] or [font=courier][email protected][/font]

Another e-mail regexp that you can try (based on [url=http://www.ietf.org/rfc/rfc822.txt]RFC822[/url]) is
/^[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+(?:.[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+)*@[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+(?:.[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+)+$/.test(formObj.email.value)
Copy linkTweet thisAlerts:
@bob_the_builderauthorOct 14.2004 — Hey Jeff Mott,

When I use:

[code=php]

else if (formObj.email.value == "" || (/^[^x00-x20()<>@,;:\".[]x7f-xff]+(?:.[^x00-x20()<>@,;:\".[]x7f-xff]+)*@[^x00-x20()<>@,;:\".[]x7f-xff]+(?:.[^x00-x20()<>@,;:\".[]x7f-xff]+)+$/.test(formObj.email.value))){
alert("You have not entered a valid email address.");
formObj.email.focus();
return false;
[/]

Based on your code .. It wont parse any email address as being valid ..?

Cheers
Copy linkTweet thisAlerts:
@Jeff_MottOct 14.2004 — The regexp returns true if the address is valid, false if not. In natural language, you want your if statement to say if e-mail is *not* valid. So, what is missing from your if statement is the not (i.e., ! ).
Copy linkTweet thisAlerts:
@bob_the_builderauthorOct 14.2004 — Hey Jeff Mott ..

Thanks for the help .. But I have no real clue on what you are saying .. as I have no javascript skills at all.

Cheers
Copy linkTweet thisAlerts:
@Jeff_MottOct 14.2004 — While we're at it why don't we put this e-mail validation into its own function to make things more organized and readable also.

String.prototype.isValidEmail = function(str)
{
return /^[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+(?:.[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+)*@[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+(?:.[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+)+$/.test(str);
}


if ( ! formObj.email.value.isValidEmail() ) {
alert("You have not entered a valid email address.");
formObj.email.focus();
return false;
}
×

Success!

Help @bob_the_builder 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...