1

jquery

function checkname(){
  //check name
}

function checkemail(){
  //check email
}

   function checkgender(){
       $("#gender-result").hide();
          if (undefined == $("input[name='gender']:checked", "#contact-form").val()) {
               $("#gender-result").html('gender requirements.').show();
               $('#gender-result').css('color', 'red');
               return false;          
          }
          return true;
     }

//jquery started
   $(document).ready(function () {
   $("#username").keyup(checkname);
   $("#gender").keyup(checkgender);
   $("#email").keyup(checkemail);
            $('#contact-form').submit(function(){
                checkgender();
                checkname();
                checkemail();
            }
        });

i am new to jquery , why is my submit wont active the function inside?? and if is good way to validation like this?

5
  • 1
    missing closing parenthesis on the next to last line. change the single line of } to }) and that should get you closer. Commented Jun 18, 2015 at 3:44
  • 1
    what is the structure of the those validation methods... does it return true/false Commented Jun 18, 2015 at 3:45
  • i edit one of my function , please check it out. Commented Jun 18, 2015 at 3:47
  • i got it ethorn thank~ careless me. Commented Jun 18, 2015 at 3:51
  • buddy you still missing the ')' parenthesis .. Commented Jun 18, 2015 at 3:58

2 Answers 2

1

Your code does in fact call the validation functions, but it submits the form immediately after, so you don't know if anything validated. If you do it this way:

$('#contact-form').submit(function () {
    if (!(checkgender() && checkname() && checkemail()){
        //tell the user they screwed up
        return false;
    }
});

When the form is submitted, it runs the validation functions. if they don't all return true, it does not submit the form, at which point you should inform the user they messed up. If the validation is successful, the form is submitted.

For your second question, yes this is a good way to do it, however, after your client side validation, you should always do additional server side validation.

...and also what @ethorn10 said, fix your missing parenthesis.

Sign up to request clarification or add additional context in comments.

6 Comments

thank you , you spark my day , i was so worry i do wrong on client side validation.
Above code wont trigger your function, form would have been already submitted, Use must use event.preventDefault()
@AdarshGowdaKR you are incorrect, if the onsubmit function returns false, it prevents the form from being submitted. event.preventDefault() AND return false; are 2 different, valid ways of preventing form submission.api.jquery.com/submit
@chiliNUT, Use must use event.preventDefault(), to prevent the default form.. Before entering your if condition, form values would have been submitted .. So to prevent that use must use event.preventDefault(), and return false is just validation thing , if that condition doesnt satisfied dont submit it.. Please read the article
@AdarshGowdaKR that is just not true at all. the form is not submitted until after the onsubmit function is executed. if the obsubmit function contains event.preventDefault() or return false; then the form is not submitted, otherwise, the form is submitted. But either way, the entire function will run first before the form is submitted.
|
0

BY default '.submit function' will submit the form, to prevent that we must use event.preventDefault() JSFIDDLE

//jquery started
   $(document).ready(function () {
   $("#username").keyup(checkname);
   $("#gender").keyup(checkgender);
   $("#email").keyup(checkemail);
            $('#contact-form').submit(function(event){
                event.preventDefault(); // preventing the default form submission
                checkgender();
                checkname();
                checkemail();
            })
        });

2 Comments

but the function wont active
Check out this link, jsfiddle.net/bam7zg73 functions get activated upon submission , remaining validation you must write it..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.