Check if the text field is empty and then run the code

I have the following jquery and you want to check if the text field is empty before the code runs:

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

but does not work.

+6
source share
3 answers

The length will never be less than 0.

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

You can even add to trim () to be thorough

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

Try

 if ($("#FNameTB").val() == '') 
+1
source

Try to execute

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

Source: https://habr.com/ru/post/896895/


All Articles