Form validation

D

DM

I'm a newbie in javascript, and came across this problem:

I have a form with several fields, and I need to validate the form. For each
field that needs validation, I include the following:

-------------------------------------
<tr><td >Email:</td><td><input type="text" name="email" value="$email"
onChange="return ValidateEmail(this);"/></td></tr>
-------------------------------------
function ValidateEmail(entered)
{

var re_email=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/

if(re_email.test(entered.value))
{
}
else {
alert("Invalid email.")
}
}
-----------------------------

It works fine... except: if I enter an invalid email it prompts me with an
error message, and continue (without changing the email address), the
validation is not done again. I tried onBlur instead of onChange, but I get
the same results.

Can anyone suggest how to avoid this problem? Thanks.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
var re_email=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/

if(re_email.test(entered.value))

That rejects (e-mail address removed) which I believe to be a permissible address;
likewise [email protected]

When validating, it is rather important that everything legitimate
should be accepted.
 
J

Julia deSilva

-------------------------------------
function ValidateEmail(entered)
{

var re_email=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/

if(re_email.test(entered.value))
{
}
else {
alert("Invalid email.")
}
}
-----------------------------

It works fine... except: if I enter an invalid email it prompts me with an
error message, and continue (without changing the email address), the
validation is not done again. I tried onBlur instead of onChange, but I get
the same results.

You have to return a boolean value from the function (true | false)
depending on whether
your email string fits the pattern. You'd also want to set the focus back to
the form element with the problem.

This should give you a structure to start things off.

function ValidateEmail(formelement) {
if (email_is_bad) {
return false;
formelement.focus();
}
else true;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top