Hello,
How do I check if a field (textbox) is empty or filled only with white spaces (spaces/enters/tabs etc.), using javascript RegExp?
Hello,
How do I check if a field (textbox) is empty or filled only with white spaces (spaces/enters/tabs etc.), using javascript RegExp?
if (myField.value.match(/\S/)) {
// field is not empty
}
Explanation, since other people seem to have some crazy different ideas:
\s
will match a space, tab or new line.
\S
will match anything but a space, tab or new line.
If your string has a single character which is not a space, tab or new line, then it's not empty.
Therefore you just need to search for one character: \S
See answers to a similar question: recommendation for javascript form validation library.
/^\s*$/.test(string)
Could be used like so:
var empty_string = /^\s*$/; //create RegExp object for re-use
if (empty_string.test(myFormField.value))
{
alert("Please be a bit more elaborate!");
}