Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2010
    Thanked 1 Time in 1 Post

    Need Email Validation and Captcha


    When I have email validation and captcha in the same form, simply passing the captcha allows the form to be sent without an email address. When I remove the captcha, the form will not send without the email address. I want to have both. What am I missing?


    function validateForm()
    var x=document.forms["WebsiteQuestion"]["Email"].value;
    var atpos=x.indexOf("@");
    var dotpos=x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
      alert("Not a valid e-mail address");
      return false;
    function clearForms()
      var i;
      for (i = 0; (i < document.forms.length); i++) {
     <div align="center">
       <script language="javascript" src="http://www.captcha.cc/cap.js?cap_k=tocsoLPahVjmEHJZWtenaJEDoJXfrQaa" type="text/javascript">
    <input type=hidden name=cap_h id=cap_h value="">
    <img border=1 id=cap_i src="http://www.captcha.cc/img.cgi?cap_k=tocsoLPahVjmEHJZWtenaJEDoJXfrQaa" align=middle><img align=middle style="margin-left: 4px;" id=cap_r onload="return cap_showReload(event);" src="http://www.captcha.cc/blank.png"><br>
    <input id=cap_t name=cap_t type=text value="" style="margin-top:4px;">
    <input type=submit value="Submit Form" onclick="return cap_valid(event);">
    I appreciate any help. Thank you, Elbee

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,573 Times in 2,551 Posts
    You probably have multiple onload statements. In which case the second script will over-write the first.

    Your email validation is long obsolete. Use this regex instead:-

    if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@((\w)([\w\-]?)+\.)+([a-z]{2,4})$/i.test(x))) {   // returns true if invalid
    If you want to allow for the very rare travel and museum domains, change {2,4} to {2,6}. If you want to allow for French lawyers change it to {2,7}.

    But in fact as most errors in emai addresses are typos etc. which cannot be detected the best email check is to request the user to enter his adddress twice, and compare the two for equality.

    Quizmaster: What name is shared by rivers in Russia and in South Yorkshire?
    Contestant: The Tiber

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Elbee (05-22-2012)


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts