Chris
Chris

Reputation: 27384

jQuery validate doesn't validate an input on keyup until it has lost focus once

I have a form with validation using jQuery validation. This works perfectly however, my form always contains default values:

<input type="text" value="0" name="myname" class="required number error">

When a user first changes the values in the input boxes, no validation occurs until the input loses focus. After this first time any keyup / change event inside the input will result in validation.

How can I force the validation to occur on keyup / change ALL the time as opposed to after an initial change.

Upvotes: 1

Views: 2408

Answers (4)

devdRew
devdRew

Reputation: 4571

As a quick answer put $('#yourForm input, #yourForm select').live('keyup change', function(e) { }) listener to your form and then check if it's the first time input is affected, and then force validate it by $('#yourForm").validate();

Upvotes: 0

user1308526
user1308526

Reputation: 11

Try this block after the .validate() function:

$('input').keyup(function(){
    if(!$(this).valid()){
        return false;
    }
});

Upvotes: 1

Pierre de LESPINAY
Pierre de LESPINAY

Reputation: 46178

Could you put your js part for the validate() initialisation ?

Did you try something like

$('#your_form input').keyUp(function () {
  $(this).parents('form').validate();
});

Upvotes: 0

Popescu Razvan
Popescu Razvan

Reputation: 159

use "onkeypress" event instead "onchange" event .

Upvotes: 1

Related Questions