6

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.

3 Answers 3

13

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 ) 
Sign up to request clarification or add additional context in comments.

1 Comment

Indeed. Less than 0 isn't possible here which is what your condition is testing.
1

Try

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

Comments

0

Try the following

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.