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 8 of 8
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Javascript validation not working on Form

    Hello:

    I am using javascript validation for submitting a form. All the other fields are working except for the Terms of Use checkbox. It sends and alert and says it wasn't checked whether or not you check it. It also submits the form whether or not it is checked immediately after the alert. The field is called df_Terms. Can anyone see anything I am missing?

    Code:
     
    <script>
    
        //submit after validation functionality
        function submit_onclick() {
    
            var proceed = false;
            if (typeof validateForm == "function") {
    
                proceed = validateForm();
            }
            else {
    
                proceed = true;
            }
            return proceed;
        }
    
        //validation functionality
    
        function validateForm() {
    
    
    
    
    
            if ((document.form1.df_Plans[0].checked == false) && (document.form1.df_Plans[1].checked == false)) {
                alert("Please select Your Plan");
                document.form1.df_Plans[0].focus();
                return false;
            }
    
    
            if ("" == document.form1.df_first_Name1.value) {
    
                alert("Please enter your First Name");
                document.form1.df_first_Name1.focus();
                return false;
            }
    
            if ("" == document.form1.df_last_Name1.value) {
    
                alert("Please enter your Last Name.");
                document.form1.df_last_Name1.focus();
                return false;
            }
    
            if ("" == document.form1.df_email1.value) {
    
                alert("Please enter your email.");
                document.form1.df_email1.focus();
                return false;
            }
    
            validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
            strEmail = document.form1.df_email1.value;
    
            if (strEmail.search(validRegExp) == -1) {
                alert("A valid e-mail address is required.");
                document.form1.df_email1.focus();
                return false;
            }
    
            if (!document.form1.df_Terms.checked == 1) {
    
                alert("Please agree to the Terms of Use.");
                document.form1.df_Terms.focus();
                return false;
            }
    
    
        }
    
        //reset field functionality
        function reset_onclick() {
    
            for (i = 0; i < document.form1.elements.length; i++) {
    
                if (document.form1.elements[i].type == "text") {
    
                    document.form1.elements[i].value = "";
                }
                if (document.form1.elements[i].type == "checkbox") {
    
                    document.form1.elements[i].checked = false;
                }
                if (document.form1.elements[i].type == "radio") {
    
                    document.form1.elements[i].checked = false;
                }
                if (document.form1.elements[i].type == "select-one") {
    
                    document.form1.elements[i].selectedIndex = 0;
                }
            }
        }
    
        //redirect functionality
        function redirect_onclick(urlString) {
    
            window.open("http://" + urlString);
        }
    
    </script>
    Last edited by developerlili; 01-04-2011 at 07:52 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    if (!document.form1.df_Terms.checked == 1) {

    Form validation of the pattern if (document.forms[0].elements[1].value == "") is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

    BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.

  • #3
    New Coder
    Join Date
    Jan 2011
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    if (!document.form1.df_Terms.checked == 1) {

    Form validation of the pattern if (document.forms[0].elements[1].value == "") is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

    BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.
    Hi Thanks for the response. Can you please assist with what the proper syntax should be?

  • #4
    New Coder
    Join Date
    Jan 2011
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I have read the forums and tried a couple of solutions but i am still having no luck.

    I changed the following line.

    Code:
     if (document.form1.df_Terms.checked == false) {
    
                alert("Please agree to the Terms of Use.");
                document.form1.df_Terms.focus();
                return false;
            }

  • #5
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by developerlili View Post
    I have read the forums and tried a couple of solutions but i am still having no luck.

    I changed the following line.

    Code:
     if (document.form1.df_Terms.checked == false) {
    
                alert("Please agree to the Terms of Use.");
                document.form1.df_Terms.focus();
                return false;
            }
    Technically there's nothing wrong with if (!document.form1.df_Terms.checked == 1) since boolean true evaluates to 1.
    Can you confirm:
    • There are no console errors?
    • Your checkbox is actually part of the form that you haven't shown?
    • The referenced element name is a case-sensitive match?

  • #6
    New Coder
    Join Date
    Jan 2011
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logic Ali View Post
    Technically there's nothing wrong with if (!document.form1.df_Terms.checked == 1) since boolean true evaluates to 1.
    Can you confirm:
    • There are no console errors?
    • Your checkbox is actually part of the form that you haven't shown?
    • The referenced element name is a case-sensitive match?
    Hi:
    This is the html code for the form
    Code:
       <td height="30"><input value="true" type="checkbox" name="df_Terms" /><input type="hidden" name="df_Terms" />
                            <td height="30" align="left">I agree to the Terms of Use.<strong><span style="color: red"> *</span></strong></td>

  • #7
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by developerlili View Post
    Hi:
    This is the html code for the form
    Code:
       <td height="30"><input value="true" type="checkbox" name="df_Terms" /><input type="hidden" name="df_Terms" />
                            <td height="30" align="left">I agree to the Terms of Use.<strong><span style="color: red"> *</span></strong></td>
    Now that you've shown the form markup, the problem is blatantly obvious. You have two elements with the same name.

  • Users who have thanked Logic Ali for this post:

    developerlili (01-04-2011)

  • #8
    New Coder
    Join Date
    Jan 2011
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logic Ali View Post
    Now that you've shown the form markup, the problem is blatantly obvious. You have two elements with the same name.
    You were absolutely right! Not sure why i didn't catch that one. This issue was escalated to me from another developer who wrote the html code. Thanks for your help.


  •  

    Posting Permissions

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