Monday, October 28, 2019

javascript - Checking the input if its empty




I am pretty new to jQuery so I have a problem with this.
I am trying to check if input is empty by jQuery. I have this script:





$('#submit').click(function(){
if($.trim($('#username').val()) == ''){
alert('Input can not be left blank');

}
});








Thanks for reply.



Answer



Check the length property to determine if it's empty:





if ($.trim($('#username').val()).length > 0) {
// not empty
}






Edit:
Removed white spaces $.trim


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...