Monday 28 October 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...