user3378165 user3378165 - 1 year ago 108
Javascript Question

Jquery validation triggers but doesn't prevent the form submitting

I have an MVC project with Knockout.

I'm trying to validate my Resiter View using jquery validation.

This is the Register View:

<form data-bind="submit: register" id="formRegister">
<input id="registerEmail" name="registerEmail" class="required email form-control" type="text" data-bind="value: registerEmail" />

<input id="registerPassword" name="registerPassword" class="required form-control" type="password" data-bind="value: registerPassword" />

<label>Confirm Password</label>
<input id="registerPassword2" name="registerPassword2" class="required form-control" type="password" data-bind="value: registerPassword2" />

<button type="submit" class="btn btn-default">Register</button>

And the jquery:

$(function () {
$(document).ready(function () {
$.extend($.validator.messages, { required: "*", email: "Invalid email address." });

When I submit the form, the validaton is triggered but the form is also being submited.

Please advise.

Answer Source

You have to return false if form is not valid.

$(document).on("submit", "#formRegister", function(evt) {
      if(!$( { return false; }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download