How do I determine if form field is empty with jQuery?

Posted by user338413 on Stack Overflow See other posts from Stack Overflow or by user338413
Published on 2010-05-11T15:33:17Z Indexed on 2010/05/11 16:34 UTC
Read the original article Hit count: 242

Filed under:
|
|
|

I've got a form with two fields, firstname and lastname. The user does not have to fill in the fields. When the user clicks the submit button, a jquery dialog displays with the data the user entered in the form. I only want to show the data for the fields that were entered. I'm trying to do an if statement and use the length() function but it isn't working. Help would be great!

Here is my dialog jquery script:

$(function(){
    //Initialize the validation dialog
    $('#validation_dialog').dialog({
        autoOpen: false,
        height: 600,
        width: 600,
        modal: true,
        resizable: false,
        buttons: {
            "Submit Form": function() {
                document.account.submit();
            },
            "Cancel": function() {
                $(this).dialog("close");
            }
        }
    });
    // Populate the dialog with form data
    $('form#account').submit(function(){
        $("p#dialog-data").append('<span>First Name: </span>');
        $("p#dialog-data").append('<span>');
        $("p#dialog-data").append($("input#firstname").val());
        $("p#dialog-data").append('</span><br/>');
        if (("input#lastname").val().length) > 0) {
            $("p#dialog-data").append('<span>Last Name: </span>');
            $("p#dialog-data").append('<span>');
            $("p#dialog-data").append($("input#lastname").val());
            $("p#dialog-data").append('</span><br/>');
        };
        $('#validation_dialog').dialog('open');
        return false;
    });
});

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about append