Reputation: 9267
I tried using following method to ban users from leaving only space in the input area with no luck:
method one:
var formSub = $('#formsub').val();
if (formSub == null || formSub == "") {
return false;
}
method two:
if (formSub.trim() == "" || formSub.trim() == " ") {
return false;
}
method three:
if ($.trim(formSub) == "" || $.trim(formSub) == " ") {
return false;
}
Any thought? :)
Upvotes: 1
Views: 66
Reputation: 2030
Haha! This is a good one you actually walk around the solution all along.
So here for example, you actually already trimmed all the spaces by using .trim()
if (formSub.trim() == "" || formSub.trim() == " ") {
return false;
}
Correct would be just,
if (formSub == " ") {
return false;
}
This will be the most useful, it passes if someone actually wrote something different from spaces, or other invisible characters ;) Google "Javascript Regex for more info"
if (/\S/.test(formSub)) {
// String is not empty
}
else{
// String is empty and not usefull
}
Cheers ;) ! +1 Appreciated
Upvotes: 0
Reputation:
Use a simple regexp:
/\S/.test(formSub)
where \S
refers to any non-white space character.
This removes the dependency on trim
(not found in IE<=8) and/or jQuery.
Upvotes: 2
Reputation: 1912
It should be formSub == null || formSub.trim() === ""
.
===
and ==
isn't exactly the same. == ""
can means true
, and any string is "true".
Upvotes: 1
Reputation: 66817
Try:
if (formValue.length === 0 || !formValue.trim()) {
return false;
}
Upvotes: 0