I have the following code:
var inp = $("#txt"); if(inp.val() != "") // do something
Is there any other way to check for empty textbox using the variable 'inp'
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.
To check if the input text box is empty using jQuery, you can use the . val() method. It returns the value of a form element and undefined on an empty collection.
when you do not enter anything in INPUT box then its value become UNDEFINED.
if (inp.val().length > 0) { // do something }
if you want anything more complicated, consider regex or use the validation plugin which takes care of this for you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With