By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,235 Members | 1,596 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,235 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a

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

P: n/a
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

P: n/a

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.