What is the easiest way to check in Javascript whether the input text field is empty (contains nothing or white spaces only)?
Yes, thanks !Actually jQuery has a built-in trim() function !
Misha Moroshko
2010-04-18 12:58:11
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.http://docs.jquery.com/Plugins/Validation/blank
calumbrodie
2010-04-18 13:19:05
+1 You are right. I should have mentioned that. Thanks for the :blank !
Misha Moroshko
2010-04-18 15:22:41
+1
A:
Include 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
}
calumbrodie
2010-04-18 12:48:25
+1
A:
var str = document.getElementById("myInput").value;
if (str.match(/^\s*$/)) {
// nothing, or nothing but whitespace
} else {
// something
}
karim79
2010-04-18 12:54:09