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

W

whisher

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 ;)
 
E

Evertjan.

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]
 
R

RobG

whisher said:
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?
}
 
V

VK

whisher said:
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));
}
 
W

whisher

VK said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top