开发者

How to display error onblur when a field is empty using jQuery Validate?

开发者 https://www.devze.com 2023-02-22 23:34 出处:网络
I\'m using jQuery Validate to validate a form. The onfocusout parameter is set to true, however it says this:

I'm using jQuery Validate to validate a form. The onfocusout parameter is set to true, however it says this:

Validate elements (except checkboxes/radio buttons) on blur. If nothing is entered, all rules are skipped, except when the field was already marked as invalid.

I cannot find anyway to make it so if the user clicks inside the field and doesn't enter anything then the error message will be displayed and the field would be highlight开发者_高级运维ed in red.

The only way it will display the error if the field is empty is if I enter text, then delete it. I need it to show the error if I click inside it then click out without entering anything at all.

Can someone tell me how to do this?


The plugin in question has a function specifically to run validation immediately on one particular element called 'element.' You can use this within the onfocusout function to force validation of that particular element.

Example:

$('form').validate({
    onfocusout: function(e) {
        this.element(e);
    }
});

See jsfiddle demo.


I came to this post after I encountered the same issue filling out, then clearing a required form field. I was using an example merging twitter bootstrap form mark up with jquery.validate.js found here: http://alittlecode.com/jquery-form-validation-with-styles-from-twitter-bootstrap/

I solved it using the following code:

onfocusout: function(label) {
        if($(label).val() == ''){
            $(label).closest('.control-group').removeClass('success'); 
        }
    }

Here's the full script in context:

$(document).ready(function() {
    $('#contact_form').validate({
    rules: {
      name: {
        minlength: 2,
        required: true
      },
      email: {
        required: true,
        email: true
      },
      message: {
        minlength: 2,
        required: true
      }
    },
    onfocusout: function(label) {
        if($(label).val() == ''){
            $(label).closest('.control-group').removeClass('success'); 
        }
    },
    highlight: function(label) {
        $(label).closest('.control-group').addClass('error');
    },
    success: function(label) {
        label
            .text('OK!').addClass('valid')
            .closest('.control-group').addClass('success');
    },
    submitHandler: function(form) {
            $(form).ajaxSubmit({
                target: '#success', 
                success: function() { 
                    $('#contact_form_wrapper').hide();
                    $('#success').html('<h3>Thank you for your enquiry</h3><p class="intro-text">We will endeavour to respond to your message within 72 hours.</p>').fadeIn('slow'); 
                } 
            });         
        }
  });
});


add class"required" to your input so it looks like

<input type="text" class="required" />

This will make the field required and on blur will fire an error if field is left empty.


Try this :

$('input').blur(function(){
    if($(this).val() == ''){
        alert('Enter something');//here you call your desired process   
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号