locked
Validation code not firing RRS feed

  • Question

  • User1216627406 posted

    Greetings mates.

    I have used this script before on page with Next, Previous, Submit code and it worked then.

    Now, I am trying to use it on a similar application where users will fill the first page and click Next to go to the next page.

    Usually, if a required field is not completed and the user clicks Next, a message will display warning the user that a required field must be filled up with the message displaying next to that field.

    With the script, you could click Next without completing a required field and it will go to the next page.

    You could even click submit and your data will submit without being asked to complete required fields.

    What am I doing wrong?

         <script type="text/javascript">                
            $(document).ready(function () {              
                $("#msform").validate({                 
                    rules: {  
                      certsel: "required",  
                      ssn1: "required",  
                      ssn2: "required",  
                      ssn3: "required",  
                      fname: "required",  
                      lname: "required",  
                      email: {  
                          required: true,
                          email: true
                        },
                      address: "required",  
                      city: "required",  
                      county: "required",  
                      state: "required",  
                      zip: "required",  
                      birthmon: "required",  
                      birthday: "required",  
                      birthyr: "required",  
                      ed: "required",  
                      ref1name: "required",  
                      ref1loc: "required",  
                      ref1title: "required",  
                      ref1area: "required",  
                      ref1pre: "required",  
                      ref1suff: "required",  
                      ref2name: "required",  
                      ref2loc: "required",  
                      ref2title: "required",  
                      ref2area: "required",  
                      ref2pre: "required",  
                      ref2suff: "required",  
                      ref3name: "required",  
                      ref3loc: "required",  
                      ref3title: "required",  
                      ref3area: "required",  
                      ref3pre: "required",  
                      ref3suff: "required",  
                      convictions: "required",  
                      txtCaptcha: "required",  
                      nmon: "required",  
                      nday: "required",  
                      nyr: "required",  
                      fullname: "required"  
                      }
                  },  
                  messages: {  
                      //This section we need to place our custom validation message for each control.  
                      certsel: "Please select the position for which you're applyingd.",
                        ssn1: "Please enter your complete Social Security Number.",  
                        ssn2: "Please enter phone number.",  
                        ssn3: "Please enter your name.", 
                        fname: "Please enter your first and last names.",  
                        lname: "Please enter your first and last names.",  
                        email: "Please enter your email address.",  
                        address: "Please enter your complete Address, including city, state and zip code.",  
                        city: "Please enter your complete Address, including city, state and zip code.",  
                        county: "Please enter your complete Address, including city, state and zip code.",  
                        state: "Please enter your complete Address, including city, state and zip code.",  
                        zip: "Please enter your complete Address, including city, state and zip code.", 
                        birthmon: "Please enter your birth month, day and year.",  
                        birthday: "Please enter your birth month, day and year.",  
                        birthyr: "Please enter your birth month, day and year.",  
                        ed: "Please enter your highest level of education or if you have a GED.",  
                        ref1name: "Please enter all information for refrence 1.",  
                        ref1loc: "Please enter all information for refrence 1.",  
                        ref1title: "Please enter all information for refrence 1.",  
                        ref1area: "Please enter all information for refrence 1.",  
                        ref1pre: "Please enter all information for refrence 1.",  
                        ref1suff: "Please enter all information for refrence 1.",  
                        ref2name: "Please enter all information for refrence 2.",  
                        ref2loc: "Please enter all information for refrence 2.",  
                        ref2title: "Please enter all information for refrence 2.",  
                        ref2area: "Please enter all information for refrence 2.",  
                        ref2pre: "Please enter all information for refrence 2.",  
                        ref2suff: "Please enter all information for refrence 2.",  
                        ref3name: "Please enter all information for refrence 3.",  
                        ref3loc: "Please enter all information for refrence 3.",  
                        ref3title: "Please enter all information for refrence 3.",  
                        ref3area: "Please enter all information for refrence 3.",  
                        ref3pre: "Please enter all information for refrence 3.",  
                        ref3suff: "Please enter all information for refrence 3.",  
                        convictions: "Please indicate whether you have had any convictions.",  
                        txtCaptcha: "Please enter the code in the space provided.",  
                        nmon: "Please enter today's date and your full name in the space provided.",  
                        nday: "Please enter today's date and your full name in the space provided.",  
                        nyr: "Please enter today's date and your full name in the space provided.",  
                        fullname: "Please enter today's date and your full name in the space provided."  
                        }
                   });  
               }); 
         if ((!$('#msform').valid())) { 
            return false;
        }  		 
        </script>

    Many thanks in advance for your help

    Friday, April 26, 2019 2:16 PM

Answers

  • User753101303 posted

    Hi,

    Use F12 network or F12 console in your browser to see what really happens. Reading code to find what happens is still trying to guess. For example here, nothing tells me that jquery is even loaded, or names could be wrong maybe in your markup or I could miss a bracket when reading etc...

    You should always first see what happens EXACTLY. And ONLY then you can read the code (at least if the problem is really in the code) which is much more efficient when knowing what to look at...

    BTW you check if the form is valid from script block ?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 26, 2019 2:27 PM

All replies

  • User753101303 posted

    Hi,

    Use F12 network or F12 console in your browser to see what really happens. Reading code to find what happens is still trying to guess. For example here, nothing tells me that jquery is even loaded, or names could be wrong maybe in your markup or I could miss a bracket when reading etc...

    You should always first see what happens EXACTLY. And ONLY then you can read the code (at least if the problem is really in the code) which is much more efficient when knowing what to look at...

    BTW you check if the form is valid from script block ?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 26, 2019 2:27 PM
  • User1216627406 posted

    Please forgive me for not use the F12.

    I did just that now and it turns out the jquery validate library was missing at the <head> of the page.

    As soon as I added that, the messages started to display.

    The only issue is that I would like the message to display after user skips a required field.

    Any ideas what to add to the script?

    Thanks for the prompt response.

    Friday, April 26, 2019 2:47 PM
  • User1216627406 posted

    I added a handler and now it works.

                $("#msform").sumbit(function(evt){
                if (!$(this).valid()) {
                evt.preventDefault();
                }
              });	

    Thanks a lot for all your help.

    Friday, April 26, 2019 3:05 PM
  • User753101303 posted

    No problem. As you see it is just simpler to see what happens rather than trying to guess from the code ;-)

    It's been a while but if I remember calling validate should be enough to "configure" the form and handling correctly the submit event. I would have to check.

    If using MVC you could also have a look at https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-2.2#client-side-validation (you have a thin validation layer doing the right thing for youu behind the scene)

    Friday, April 26, 2019 3:12 PM