/    Sign up×
Community /Pin to ProfileBookmark

Javascript email validate_form help please :)

Hey guys,

Having a little trouble validating an email address field in an existing form. I validated the other feilds so they are not left blank but cant figure the email one.

My form details are:
<form id=form name=form action=http://www.smudged.co.uk/ebay/apply.php method=post target=_blank onsubmit=”return validate_form ( );”>

Input box details are:
<input type=”text” name=”paypal” id=paypal style=”width:225px” size=”40″ />

Javascript code so far is:
<script type=”text/javascript”>
<!–
function validate_form ( )
{
valid = true;

if ( document.form.fullname.value == “” )
{
alert ( “Please enter your full name.” );
valid = false;
}

if ( document.form.addressa.value == “” )
{
alert ( “Please enter your Address Line 1.” );
valid = false;
}

if ( document.form.addressb.value == “” )
{
alert ( “Please enter your Address Line 2.” );
valid = false;
}

if ( document.form.city.value == “” )
{
alert ( “Please enter your City.” );
valid = false;
}

if ( document.form.postcode.value == “” )
{
alert ( “Please enter your Postcode.” );
valid = false;
}

if ( document.form.paypal.value == “” )
{
alert ( “Please enter your Paypal email address.” );
valid = false;
}

if ( document.form.phone.value == “” )
{
alert ( “Please enter your Phone number.” );
valid = false;
}

if ( document.form.size.selectedIndex == 0 )
{
alert ( “Please Select The T-Shirt Size Required.” );
valid = false;
}

if ( document.form.colour.selectedIndex == 0 )
{
alert ( “Please Select The T-Shirt Colour Required.” );
valid = false;
}

return valid;

}
//–>
</script>

It validates everything at the moment so that it cant be left empty but I would like it to do the email field properly.

The page its located on at the moment is [url]http://www.smudged.co.uk/ebay/new.php[/url]

Many thanks in advance.

Dean

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@web_bertAug 27.2009 — try this:

[CODE]
function validate_form ( )
{
var valid = true,
form = document.form;

if ( form.fullname.value == "" ){
alert ( "Please enter your full name." );
valid = false;
}

if ( form.addressa.value == "" ){
alert ( "Please enter your Address Line 1." );
valid = false;
}

if ( form.addressb.value == "" ){
alert ( "Please enter your Address Line 2." );
valid = false;
}

if ( form.city.value == "" ){
alert ( "Please enter your City." );
valid = false;
}

if ( form.postcode.value == "" ){
alert ( "Please enter your Postcode." );
valid = false;
}

if ( form.paypal.value == "" ){
alert ( "Please enter your Paypal email address." );
valid = false;
} else if( !( /^[A-Z0-9._&#37;+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$/i.test( form.paypal.value ) ) ) {
alert( "Please enter a valid email address");
valid = false;
}

if ( form.phone.value == "" ){
alert ( "Please enter your Phone number." );
valid = false;
}

if ( form.size.selectedIndex == 0 ){
alert ( "Please Select The T-Shirt Size Required." );
valid = false;
}

if ( form.colour.selectedIndex == 0 ){
alert ( "Please Select The T-Shirt Colour Required." );
valid = false;
}

return valid;
}
[/CODE]


There are certain addresses that the regex does not cater for, see here for more info http://www.regular-expressions.info/email.html
Copy linkTweet thisAlerts:
@DeCalauthorAug 27.2009 — Excellent, thank you so much for that, worked a treat ?

If anyone would like to check it out, its: http://www.smudged.co.uk/ebay/new.php

Thanks again for the help ?

Dean
×

Success!

Help @DeCal 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.20,
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,
)...