469,632 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,632 developers. It's quick & easy.

[REGEX VALIDATOR] Function to check a empty field or with only space .......

Hi.
I'm taking my first steps on regex I set up
this simple function to check if a form field is empty
or with only space.
var onlySpaceRegexp = /^\s*$/;
function isEmpty(val)
{

if (onlySpaceRegexp.test(val) || val == "")
{
return true;
}
else
{
return false;
}
}
alert(isEmpty(""));//TRUE
alert(isEmpty(" "));//TRUE
alert(isEmpty(" v "));//FALSE
I'm wandering is it the best way ?

Take care.
Bye ;)

Nov 23 '06 #1
4 3004
whisher wrote on 23 nov 2006 in comp.lang.javascript:
Hi.
I'm taking my first steps on regex I set up
this simple function to check if a form field is empty
or with only space.
var onlySpaceRegexp = /^\s*$/;
function isEmpty(val)
{

if (onlySpaceRegexp.test(val) || val == "")
{
return true;
}
else
{
return false;
}
}
alert(isEmpty(""));//TRUE
alert(isEmpty(" "));//TRUE
alert(isEmpty(" v "));//FALSE
I'm wandering is it the best way ?
There is no best way in programming.
That's why it is fun!

However:

1

the || val == "" is never used,
as an empty string is already detected by the test()

2

/^\s*$/.test(val)
["must be all white-space"]

is equivalent to:

!/\S/.test(val)
["not any non-white-space"]

3

your function can be written as:

function whiteSpaceOnly(val) {
return !/\S/.test(val)
}

[giving it a better name,
not testing for true/false to return another pair of the same]
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Nov 23 '06 #2

whisher wrote:
Hi.
I'm taking my first steps on regex I set up
this simple function to check if a form field is empty
or with only space.
var onlySpaceRegexp = /^\s*$/;
Pre-initialising a regexp is handy if you are going to use it more than
once, but for one-of cases, there isn't much point other than for style
or convention.
function isEmpty(val)
{

if (onlySpaceRegexp.test(val) || val == "")
The second test is completely covered by the first (which matches
strings consisting of only zero or more spaces), and therefore is
redundant.
{
return true;
}
else
There is no need for an 'else' after a conditional return, though maybe
is makes maintenance easier.
{
return false;
}
}
alert(isEmpty(""));//TRUE
alert(isEmpty(" "));//TRUE
alert(isEmpty(" v "));//FALSE
I'm wandering is it the best way ?
You might consider one of the following:

function isEmpty(val){
return /^\s*$/.test(val);
}

function isEmpty(val){
return !/\S/.test(val);
}

You might want to handle cases where val isn't a string, the following
is a start but what should be returned if val isn't a string?

function isEmpty(val){
if (typeof val == 'string'){
return /^\s*$/.test(val);
}
// val isn't as string, what now?
}

--
Rob

Nov 23 '06 #3
VK

whisher wrote:
Hi.
I'm taking my first steps on regex I set up
this simple function to check if a form field is empty
or with only space.
var onlySpaceRegexp = /^\s*$/;
function isEmpty(val)
{

if (onlySpaceRegexp.test(val) || val == "")
{
return true;
}
else
{
return false;
}
}
alert(isEmpty(""));//TRUE
alert(isEmpty(" "));//TRUE
alert(isEmpty(" v "));//FALSE
I'm wandering is it the best way ?
I usually put the question from the other end: "Is this form field not
empty?" (contains some alphanumeric characters). But your way is fine
too.

test() method returns either true (match found) or false: this way
there is no need to additionally wrap it into return true / return
false branches.

var re = /\S+/;

function isEmpty(val) {
return (!(re.test(val));
}

Nov 23 '06 #4

VK wrote:
whisher wrote:
Hi.
I'm taking my first steps on regex I set up
this simple function to check if a form field is empty
or with only space.
var onlySpaceRegexp = /^\s*$/;
function isEmpty(val)
{

if (onlySpaceRegexp.test(val) || val == "")
{
return true;
}
else
{
return false;
}
}
alert(isEmpty(""));//TRUE
alert(isEmpty(" "));//TRUE
alert(isEmpty(" v "));//FALSE
I'm wandering is it the best way ?

I usually put the question from the other end: "Is this form field not
empty?" (contains some alphanumeric characters). But your way is fine
too.

test() method returns either true (match found) or false: this way
there is no need to additionally wrap it into return true / return
false branches.

var re = /\S+/;

function isEmpty(val) {
return (!(re.test(val));
}
;) ;) ;)
Thanks a lot buddies for the enlightments
The fog is lifing ;)
Take care.
Bye.

Nov 24 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Marcel Brekelmans | last post: by
2 posts views Thread by Paul Telco | last post: by
1 post views Thread by Oleg Ogurok | last post: by
3 posts views Thread by Mad Scientist Jr | last post: by
3 posts views Thread by Joachim | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.