Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why jQuery validation doesn't work reliably?

Sometimes the unobtrusive validation appears, sometimes not.

Unobtrusive validations works reliably only when I explicitly call form's valid() function (done during submit)

Why is this so?

The code:

<script src="jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="jquery.validate.min.js" type="text/javascript"></script>
<script src="jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<form action="/User/Input" id="fxUser634377199694370000" method="post">    
  <fieldset>
    <legend>Person</legend>
    <div class="editor-label">
      <label for="Firstname">Firstname</label>
    </div>
    <div class="editor-field">
      <input class="text-box single-line" data-val="true" data-val-required="The Firstname field is required." id="Firstname" name="Firstname" type="text" value="" />
      <span class="field-validation-valid" data-valmsg-for="Firstname" data-valmsg-replace="true"></span>
    </div>
    <div class="editor-label">
      <label for="Lastneim">Lastneim</label>
    </div>
    <div class="editor-field">
      <input class="text-box single-line" data-val="true" data-val-required="The Lastneim field is required." id="Lastneim" name="Lastneim" type="text" value="" />
      <span class="field-validation-valid" data-valmsg-for="Lastneim" data-valmsg-replace="true"></span>
    </div>
    <div class="editor-label">
      <label for="Age">Age</label>
    </div>
    <div class="editor-field">
      <input class="text-box single-line" data-val="true" data-val-number="The field Age must be a number." data-val-required="The Age field is required." id="Age" name="Age" type="text" value="" />
      <span class="field-validation-valid" data-valmsg-for="Age" data-valmsg-replace="true"></span>
    </div>
    <p>
      <input type="submit" id='fxUser634377199694370000_Saver' value="Save" />
    </p>
  </fieldset>
</form>

<script type="text/javascript">
  $(function () {
    $('#fxUser634377199694370000_Saver').click(function (e) {
      e.preventDefault();
      if ($('#fxUser634377199694370000').valid())
        ;
      else {
        alert('not ok');
        return;
      }
      alert($('#fxUser634377199694370000').serialize());
    });
  });
</script>

Functional code, sans server code, only html and client-side(jquery libraries): http://www.sendspace.com/file/913i1w

like image 388
Hao Avatar asked Apr 06 '11 12:04

Hao


2 Answers

The docs say: "Before a field is marked as invalid, the validation is lazy: Before submitting the form for the first time, the user can tab through fields without getting annoying messages - he won't get bugged before he had the chance to actually enter a correct value".

Is that what bugs you?

like image 81
jammon Avatar answered Oct 20 '22 12:10

jammon


Normally i would be satisfied with lazy validation but on a very specific app this is not the case. Inspired by @Peeter i'm calling validation like:

$("#wep_key_register_test_second").focus(function() {
    $("#wep_key_register_test_second").valid();
});

and afterwards letting the plugin function normally.

like image 29
Andrew Starlike Avatar answered Oct 20 '22 14:10

Andrew Starlike