What is the easiest way to check in Javascript whether the input text field is empty (contains nothing or white spaces only)?
3 Answers
You are looking for something like trim function, right?
4 Comments
Misha Moroshko
Yes, thanks ! Actually jQuery has a built-in trim() function !
calumbrodie
you didn't say that you were working with jQuery - you get the best answers if you provide more information... the jquery validate plug provides a :blank selector. docs.jquery.com/Plugins/Validation/blank
Misha Moroshko
+1 You are right. I should have mentioned that. Thanks for the :blank !
Towkir
the link is broken now, and with current strategy, the answer is more like a
link only answerInclude this function somewhere (in order to provide a trim function)
String.prototype.trim = function () {
return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};
see here http://javascript.crockford.com/remedial.html
then...
if (document.forms['id_of_form'].elements['id_of_input'].value.trim()=='') {
//do xyz
}