Alex Rivera | Logout

Checking for blank input with jQuery

Asked 2010-02-04T06:24:07.820
14

I'm submitting some comments via $.ajax. However I want to check to make sure that some content is entered in the input box. At the moment I am using this:

if ($("#comments").val().length != 0){

However this only works if a user leaves in the input blank, if the leave a space or return then it will pass. How can I improve this if?

Edit
Report

1 Answer

2

In JavaScript

//Following all return true
"" == false
" " == false
"  " == false
"   " == false
//Not related to question but interesting
//Following all (also) return true
!" " == false
!"  " == false
!"   " == false
//Except
!"" == false //returns false

(Verified with Firebug console and Chromium's Developer Tools console).

answered 2010-08-16T12:15:14.660

Your Answer