Validation rule not working

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

    Validation rule not working

    I've ripped off some script from another site of mine that works fine which
    checks an input box to see if an email address has been entered when
    submitting. If it hasn't a prompt is issued asking the user to enter an
    email address.

    It seems to be ignoring the validation rule and just emailing the form
    anyway, can anyone help?

    Below is the the relevant code -



    From the <head> tag-

    <SCRIPT LANGUAGE="JavaS cript"> //This defines the language as javascript
    <!-- hide JS code
    function validateForm(fo rm)

    {
    // EMAIL VALIDATION

    if (!validateEMail (form.Email.val ue)) // is the email address valid?
    {
    form.Email.focu s()
    return false
    }



    if (justValidating =true) // the alert box only shows when validating, not
    submitting
    alert("Thank you. Your data has been validated and is ready to be
    submitted.") // message shown if all data is valid
    return true
    }

    // EMAIL VALIDATION RULES

    function validateEMail(E mail)
    {
    if (isBlank(Email) ) // email blank?
    {
    alert("Enter your email address, please!") // return this message if it is
    blank
    return false
    }
    var atsignPos = Email.indexOf(" @", 0) // check for @ sign
    if (atsignPos == -1)
    {
    alert("Enter a valid email address with an @, please!") // return this
    message if no @ sign present
    return false
    }
    if (Email.indexOf( ".", atsignPos) == -1) // check for full stop after @ sign
    {
    alert("Enter a valid email domain after the @, please!") //return this
    message if no full stop after @ sign
    return false
    }
    return true
    }





    // end JS hide -->
    </SCRIPT>



    And then this is the details within the form -

    <form name="Validate" action="mailto: dummyemailaddre ss.co.uk" method="post"
    onSubmit="justV alidating=false ; return validateForm(Va lidate)">

    <input type="text" name="Email">


    <input type="submit" name="Submit" value="Submit" ; validateForm(Va lidate)>

    <input type="reset" name="Reset" value="Reset"></form>






    Any help appreciated

    Thanks

    Andrew



  • Jim Dabell

    #2
    Re: Validation rule not working

    Andrew wrote:

    [snip][color=blue]
    > It seems to be ignoring the validation rule and just emailing the form
    > anyway, can anyone help?[/color]
    [snip][color=blue]
    > From the <head> tag-[/color]

    The <head> element - the code certainly doesn't reside within the tag.

    [color=blue]
    > <SCRIPT LANGUAGE="JavaS cript"> //This defines the language as javascript[/color]

    You are missing the required type attribute.

    <URL:http://www.w3.org/TR/html401/interact/scripts.html#ed ef-SCRIPT>

    [color=blue]
    > <!-- hide JS code[/color]

    Practically speaking, this does nothing, except increase the possibility for
    headaches if you plan to use XHTML in the future. In any case, it's
    usually a better idea to put your Javascript in external files.


    [snip][color=blue]
    > if (justValidating =true) // the alert box only shows when validating, not
    > submitting[/color]

    I fail to see the difference. Your form as described is only ever validated
    when it is submitted.

    [color=blue]
    > alert("Thank you. Your data has been validated and is ready to be
    > submitted.") // message shown if all data is valid
    > return true
    > }
    >
    > // EMAIL VALIDATION RULES
    >
    > function validateEMail(E mail)
    > {[/color]
    [snip]

    That is fairly convoluted code that gives false positives and false
    negatives. Why not just use a couple of regexps instead? There is some
    discussion of the issues here:

    <URL:http://blog.tom.me.uk/2003/08/03/evil_email_addr esses.php>


    [snip][color=blue]
    > <form name="Validate" action="mailto: dummyemailaddre ss.co.uk"[/color]

    mailto actions are unreliable.

    <URL:http://www.netmechanic .com/news/vol3/form_no4.htm>


    [snip][color=blue]
    > <input type="submit" name="Submit" value="Submit" ;
    > validateForm(Va lidate)>[/color]

    This isn't valid HTML, there is no Javascript seen by the browser here. The
    onsubmit attribute of the <form> element takes care of the validation when
    the form is submitted anyway.

    [color=blue]
    > <input type="reset" name="Reset" value="Reset"></form>[/color]

    <URL:http://www.useit.com/alertbox/20000416.html>


    Good places to look for hints when debugging are the Javascript console in
    Mozilla, and the HTML validator service at <URL:http://validator.w3.or g/>.

    --
    Jim Dabell

    Comment

    • Dr John Stockton

      #3
      Re: Validation rule not working

      JRS: In article <OL8%a.12441$Kx 1.184698@newsfe p4-glfd.server.ntl i.net>,
      seen in news:comp.lang. javascript, Andrew <andrew-
      dixon1971MYPANT S@ntlworld.com> posted at Fri, 15 Aug 2003 18:31:01 :-[color=blue]
      >I've ripped off some script from another site of mine that works fine which
      >checks an input box to see if an email address has been entered when
      >submitting. If it hasn't a prompt is issued asking the user to enter an
      >email address.
      >
      >It seems to be ignoring the validation rule and just emailing the form
      >anyway, can anyone help?[/color]
      [color=blue]
      > ...[/color]
      [color=blue]
      >if (justValidating =true) // the alert box only shows when validating, not[/color]
      ==
      xxxxx[color=blue]
      >submitting
      >alert("Thank you. Your data has been validated and is ready to be
      >submitted.") // message shown if all data is valid[/color]

      That's another reason for never testing equality with a Boolean
      constant; the better fix is on the second inserted line.

      One can do a little better testing E-addresses; use a RegExp to test for
      something at something dot something

      See in <URL:http://www.merlyn.demo n.co.uk/js-other.htm#VEmA> .

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

      Comment

      Working...