Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 241
PD24 Avatar asked Sep 08 '11 15:09

PD24


People also ask

How do you check if a text box is empty or not?

IsNullOrEmpty() function has a boolean return type and returns true if the string is either null or empty and otherwise returns false . We can use the String. IsNullOrEmpty() function on the string inside the TextBox. Text property to check whether the text inside the text box is empty or not.

How check TextBox is empty or not in C#?

If the textbox is empty then the IsEmpty function returns false. If the textbox contains only '1' then IsEmpty returns true.

What is the event in JavaScript that is used to check an empty box?

When the Validate Button is clicked, a JavaScript function is called and the TextBox is referenced and its value is compared with Empty string and if the condition tests TRUE then the TextBox is considered Empty.


2 Answers

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 ) 
like image 169
aziz punjani Avatar answered Nov 15 '22 01:11

aziz punjani


Try

if ($("#FNameTB").val() == '')
like image 34
Jason Gennaro Avatar answered Nov 14 '22 23:11

Jason Gennaro