PD24
PD24

Reputation: 774

check if text box is empty then run code

I have the following jquery and want to check if the text box is empty before the code is run:

<script type="text/javascript">
    $(document).ready(function () {
        if ($("#FNameTB").val().length < 0) {
            $("input#FNameTB").labelify({ labelledClass: "greylabel" });
        }       
</script>

but its not working.

Upvotes: 6

Views: 14201

Answers (3)

aziz punjani
aziz punjani

Reputation: 25776

Length will never be less than 0.

if ( $("#FNameTB").val().length === 0 ) 

You can even add in a trim() to be thorough

if ( $("#FNameTB").val().trim().length === 0 ) 

Upvotes: 13

JaredPar
JaredPar

Reputation: 755141

Try the following

if ($('#FNameTB').val() === '') { 
  // It's empty
}

Upvotes: 0

Jason Gennaro
Jason Gennaro

Reputation: 34855

Try

if ($("#FNameTB").val() == '')

Upvotes: 1

Related Questions