0

I have to implement client side validation with click event for ajax posts. I followed the below url:

Call MVC 3 Client Side Validation Manually for ajax posts

I tried using this :

$('#buttonId').click(function(evt) {
    evt.preventDefault();
    var $form = $('form');
    if($form.valid()) {
        //Ajax call here
    }
});

But the problem here is now the validation is working if there is invalid data. But if I give proper input also, the page is reloading and blank page is coming as a result.

I tried replacing evt.preventDefault() with return false. But there the validation is not working. Can anyone help me to solve this.

The thing is if drop down is not chosen, the validation message is coming. But If I give correct input, then output is not displayed.The page is refreshing.

I guess the problem is evt.preventDefault() . But validation is not working without this

7
  • just wanted to know; this buttonId, is a normal HTML button or is it a input type submit element? if it is a submit type then yes your form will get submitted the HTML form way hence reloading the page Commented Aug 20, 2015 at 8:13
  • 1
    return false; needs to be the last line in the script Commented Aug 20, 2015 at 8:14
  • show your ajax call too if possible! Commented Aug 20, 2015 at 8:15
  • preventDefault should be in the else part right? Commented Aug 20, 2015 at 8:16
  • @vijay, yes it is a input type submit Commented Aug 20, 2015 at 9:09

2 Answers 2

1

Don't use the click of the button, I would recomend to use the following. The 'submitHanderl is a callback function if the form is valid.

$("form").validate({
    submitHandler: function(form) {
        //Ajax call here
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Also, for future reference, if you add type="button", it will treat the button as a button and it will not submit the click. Same as doing an event.preventDefault() or an <a> tag's return false;

Comments

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.