Form validation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DM

    Form validation

    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="retur n ValidateEmail(t his);"/></td></tr>
    -------------------------------------
    function ValidateEmail(e ntered)
    {

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

    if(re_email.tes t(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.


  • Julia deSilva

    #2
    Re: Form validation

    > -------------------------------------[color=blue]
    > function ValidateEmail(e ntered)
    > {
    >
    > var re_email=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,} )+$/
    >
    > if(re_email.tes t(entered.value ))
    > {
    > }
    > else {
    > alert("Invalid email.")
    > }
    > }
    > -----------------------------
    >
    > It works fine... except: if I enter an invalid email it prompts me with[/color]
    an[color=blue]
    > error message, and continue (without changing the email address), the
    > validation is not done again. I tried onBlur instead of onChange, but I[/color]
    get[color=blue]
    > the same results.[/color]

    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(f ormelement) {
    if (email_is_bad) {
    return false;
    formelement.foc us();
    }
    else true;
    }



    Comment

    • Dr John Stockton

      #3
      Re: Form validation

      JRS: In article <Rglpb.1247$mB5 .217532@news20. bellglobal.com> , seen in
      news:comp.lang. javascript, DM <dm@nospam.co m> posted at Sun, 2 Nov 2003
      23:59:28 :-[color=blue]
      >
      >var re_email=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,} )+$/
      >
      >if(re_email.te st(entered.valu e))[/color]

      That rejects j+a@b.cc which I believe to be a permissible address;
      likewise j$a@b.cc

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

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
      Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
      Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
      Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

      Comment

      Working...