Check if inputs are empty using jQuery

前端 未结 18 907
灰色年华
灰色年华 2020-11-22 09:14

I have a form that I would like all fields to be filled in. If a field is clicked into and then not filled out, I would like to display a red background.

Here is my

相关标签:
18条回答
  • 2020-11-22 09:34

    Doing it on blur is too limited. It assumes there was focus on the form field, so I prefer to do it on submit, and map through the input. After years of dealing with fancy blur, focus, etc. tricks, keeping things simpler will yield more usability where it counts.

    $('#signupform').submit(function() {
        var errors = 0;
        $("#signupform :input").map(function(){
             if( !$(this).val() ) {
                  $(this).parents('td').addClass('warning');
                  errors++;
            } else if ($(this).val()) {
                  $(this).parents('td').removeClass('warning');
            }   
        });
        if(errors > 0){
            $('#errorwarn').text("All fields are required");
            return false;
        }
        // do the ajax..    
    });
    
    0 讨论(0)
  • 2020-11-22 09:37
    $('#apply-form input').blur(function()
    {
        if( !$(this).val() ) {
              $(this).parents('p').addClass('warning');
        }
    });
    

    And you don't necessarily need .length or see if it's >0 since an empty string evaluates to false anyway but if you'd like to for readability purposes:

    $('#apply-form input').blur(function()
    {
        if( $(this).val().length === 0 ) {
            $(this).parents('p').addClass('warning');
        }
    });
    

    If you're sure it will always operate on a textfield element then you can just use this.value.

    $('#apply-form input').blur(function()
    {
          if( !this.value ) {
                $(this).parents('p').addClass('warning');
          }
    });
    

    Also you should take note that $('input:text') grabs multiple elements, specify a context or use the this keyword if you just want a reference to a lone element (provided there's one textfield in the context's descendants/children).

    0 讨论(0)
  • 2020-11-22 09:37

    Everybody has the right idea, but I like to be a little more explicit and trim the values.

    $('#apply-form input').blur(function() {
         if(!$.trim(this.value).length) { // zero-length string AFTER a trim
                $(this).parents('p').addClass('warning');
         }
    });
    

    if you dont use .length , then an entry of '0' can get flagged as bad, and an entry of 5 spaces could get marked as ok without the $.trim . Best of Luck.

    0 讨论(0)
  • 2020-11-22 09:40

    Consider using the jQuery validation plugin instead. It may be slightly overkill for simple required fields, but it mature enough that it handles edge cases you haven't even thought of yet (nor would any of us until we ran into them).

    You can tag the required fields with a class of "required", run a $('form').validate() in $(document).ready() and that's all it takes.

    It's even hosted on the Microsoft CDN too, for speedy delivery: http://www.asp.net/ajaxlibrary/CDN.ashx

    0 讨论(0)
  • 2020-11-22 09:40

    There is one other thing you might want to think about, Currently it can only add the warning class if it is empty, how about removing the class again when the form is not empty anymore.

    like this:

    $('#apply-form input').blur(function()
    {
        if( !$(this).val() ) {
              $(this).parents('p').addClass('warning');
        } else if ($(this).val()) {
              $(this).parents('p').removeClass('warning');
        }
    });
    
    0 讨论(0)
  • 2020-11-22 09:42

    The keyup event will detect if the user has cleared the box as well (i.e. backspace raises the event but backspace does not raise the keypress event in IE)

        $("#inputname").keyup(function() {
    
    if (!this.value) {
        alert('The box is empty');
    }});
    
    0 讨论(0)
提交回复
热议问题