Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Resolved: validate field

    Hello hello,

    I was wondering if anyone could help me out with this?

    A friend wrote me a nice little script to validate checkboxes for a quiz I was making.. problem on success and submit of the form, the email field isn't validated.

    [CODE]

    function validate(){

    var questions = test.question; //Creates an array of elements from form "test" with the name "question"
    cor_count = 0; //Counts the number of correct answers selected
    for (var i=0; i<questions.length; i++) if (questions[i].value == "correct" && questions[i].checked) cor_count++;
    if (cor_count==10)

    {
    alert("Success: submitting entry");
    return true;
    }
    else alert("You selected " + cor_count + " correct species. You may try again.")
    return false;
    }

    function adjust() {
    questions = test.question; //Creates an array of elements from form "test" with the name "question"
    sel_count = 0; //Counts the number of check boxes the user has selected
    for (var i = 0; i<questions.length; i++) if (questions[i].checked) sel_count++;
    if (sel_count==10) {
    for (var i=0; i<questions.length; i++) if (!questions[i].checked) questions[i].disabled=true;
    test.submit.disabled=false;
    }
    else {
    for (var i=0; i<questions.length; i++) questions[i].disabled=false;
    test.submit.disabled=true;
    }

    }

    [CODE]

    Obviously I have a form (just built in pain html) that has a 'email' text field whole bunch of checkboxes and a submit button.

    If anyone could help me validate that email field that would be awesome.

    Thanks
    Last edited by miknq; 09-21-2005 at 01:29 AM. Reason: Resloved

  • #2
    New Coder
    Join Date
    Sep 2005
    Location
    Ottawa, ON
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's the regular expression I normally use for basic email validation:
    /(^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$)/

  • #3
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    A little more help please...

    Hi Thanks for your reply...

    Could you please tell me where exactly I should be placing this code.. i'm not much of a JS coder.. a friend wrote me the code I posted..

    Should the email validation be placed inside the code I posted or should it be something separate?

    Thanks very much

  • #4
    JUD
    JUD is offline
    New Coder
    Join Date
    Jul 2005
    Location
    I'm right here
    Posts
    92
    Thanks
    0
    Thanked 2 Times in 2 Posts
    You could put it in your if statement that checks if all the questions are right

    Like this :
    Code:
    function validate(){
    
    var questions = test.question; //Creates an array of elements from form "test" with the name "question"
    cor_count = 0; //Counts the number of correct answers selected
    for (var i=0; i<questions.length; i++) if (questions[i].value == "correct" && questions[i].checked) cor_count++;
    var emailRegExp = /^[a-zA-Z0-9]+[\w\.-]*@[a-zA-Z0-9]+[\w\.-]*[a-zA-Z]+$/;
    if (cor_count==10) 
    
    {
    if(!emailRegExp.test(document.yourForm.yourEmailField.value)){
    alert("Please enter a valid email address");
    return false;
    }
    alert("Success: submitting entry");
    return true;
    }
    else alert("You selected " + cor_count + " correct species. You may try again.")
    return false;
    }

  • #5
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Still not working..

    Hi thanks heaps for your reply.. very much appreciated..

    However I still can't seam to get this working.. with the ammendments you gave me I have a page with the following..

    Code:
    <script language="JavaScript">
    function validate(){
    
    var questions = test.question; //Creates an array of elements from form "test" with the name "question"
    cor_count = 0; //Counts the number of correct answers selected
    for (var i=0; i<questions.length; i++) if (questions[i].value == "correct" && questions[i].checked) cor_count++;
    var emailRegExp = /^[a-zA-Z0-9]+[\w\.-]*@[a-zA-Z0-9]+[\w\.-]*[a-zA-Z]+$/;
    if (cor_count==10) 
    
    {
    if(!emailRegExp.test(document.test.email.value)){
    alert("Please enter a valid email address");
    return false;
    }
    alert("Success: submitting entry");
    return true;
    }
    else alert("You selected " + cor_count + " correct species. You may try again.")
    return false;
    }
    </script>
    <form name="test" action="/form/challengemail.php" method="post" target="_self" onsubmit="return validate()">
                  <table WIDTH="95%" border = "0" align = "center" cellpadding="1" cellspacing="1">
                    <tr> 
                      <td WIDTH="5%"> <input type="checkbox" name="question" value="correct" onclick="adjust()"> 
                        <br> </td>
                      <td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Coronation 
                          Trout</div></td>
                      <td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="incorrect"> 
                        <br> </td>
                      <td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Dhufish</div></td>
                      <td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="incorrect"> 
                        <br> </td>
                      <td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Coral 
                          Cod</div></td>
                      <td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="incorrect"> 
                        <br> </td>
                      <td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Marlin 
                          Blue </div></td>
                    </tr>
                    <tr> 
                      <td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="incorrect"> 
                        <br> </td>
                      <td class="bodytxt"> <div align="left" class="bodytxt">Golden 
                          Perch</div></td>
                      <td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="incorrect"> 
                        <br> </td>
                      <td width="20%" class="bodytxt"> <div align="left">Barramundi</div></td>
                      <td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="correct"> 
                        <br> </td>
                      <td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Mangrove 
                          Jack</div></td>
                      <td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="correct"> 
                        <br> </td>
                      <td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Spangled 
                          Emperor </div></td>
                    </tr>
                    <tr> 
                      <td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="incorrect"> 
                        <br> </td>
                      <td class="bodytxt"> <div align="left" class="bodytxt">Saratoga</div></td>
                      <td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="correct"> 
                        <br> </td>
                      <td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Albacore</div></td>
                      <td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="incorrect"> 
                        <br> </td>
                      <td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Bonito</div></td>
                      <td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="incorrect"> 
                        <br> </td>
                      <td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Sailfish</div></td>
                    </tr>
                    <tr> 
                      <td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="correct"> 
                        <br> </td>
                      <td class="bodytxt"> <div align="left" class="bodytxt">Spanish 
                          Mackerel</div></td>
                      <td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="incorrect"> 
                        <br> </td>
                      <td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Stripey</div></td>
                      <td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="correct"> 
                        <br> </td>
                      <td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Blue 
                          Groper</div></td>
                      <td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="correct"> 
                        <br> </td>
                      <td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Catfish 
                          Fork-tailed</div></td>
                    </tr>
                    <tr> 
                      <td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="correct"> 
                        <br> </td>
                      <td class="bodytxt"> <div align="left" class="bodytxt">John 
                          Dory</div></td>
                      <td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="correct"> 
                        <br> </td>
                      <td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Leatherjacket</div></td>
                      <td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="incorrect"> 
                        <br> </td>
                      <td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Red 
                          Bass</div></td>
                      <td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="correct"> 
                        <br> </td>
                      <td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Golden 
                          Trevally</div></td>
                    </tr>
                    <tr> 
                      <td COLSPAN="8" ALIGN="center"> <div align="right">&nbsp;&nbsp;&nbsp;<br>
                          <span class="bodytxt"> Email:</span> 
                          <input name="email" type="text" id="email">
                          <input type="submit" name="submit" value="Submit">
                          &nbsp;&nbsp;&nbsp;<span class="bodytxt">
                          
                          </span>&nbsp; </div></td>
                    </tr>
                  </table><script>adjust();</script>
                  </form>
    Thanks very much if you can help me out any furthur..

    Mik


  •  

    Posting Permissions

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