jQuery - simple input validation - "empty" and "not empty" jQuery - simple input validation - "empty" and "not empty" ajax ajax

jQuery - simple input validation - "empty" and "not empty"


JQuery's :empty selector selects all elements on the page that are empty in the sense that they have no child elements, including text nodes, not all inputs that have no text in them.

Jquery: How to check if an input element has not been filled in.

Here's the code stolen from the above thread:

$('#apply-form input').blur(function()          //whenever you click off an input element{                       if( !$(this).val() ) {                      //if it is blank.          alert('empty');        }});

This works because an empty string in JavaScript is a 'falsy value', which basically means if you try to use it as a boolean value it will always evaluate to false. If you want, you can change the conditional to $(this).val() === '' for added clarity. :D


You could do this

$("#input").blur(function(){    if($(this).val() == ''){        alert('empty');     }});

http://jsfiddle.net/jasongennaro/Y5P9k/1/

When the input has lost focus that is .blur(), then check the value of the #input.

If it is empty == '' then trigger the alert.


    jQuery("#input").live('change', function() {        // since we check more than once against the value, place it in a var.        var inputvalue = $("#input").attr("value");        // if it's value **IS NOT** ""        if(inputvalue !== "") {            jQuery(this).css('outline', 'solid 1px red');         }           // else if it's value **IS** ""        else if(inputvalue === "") {            alert('empty');         }    });