Empty input box

P

Paul Lautman

Hi y'all,
I found this function at
http://www.devshed.com/c/a/JavaScript/Form-Validation-with-JavaScript/6/

// check to see if input is whitespace only or empty
function isEmpty(val)
{
if (val.match(/^s+$/) || val == "")
{
return true;
}
else
{
return false;
}
}

When I tried it it didn't work, but I found that I could make it work by
changing the regex to /^\s+$/
I then tried shortening it to:

function isEmpty(val)
{
return (val.match(/^s+$/) || val == "");
{

But this returns the value matched (when it is spaces).

Can someone answer the following 2 questions for me:

1) I'm sure that the function worked for the author (Nariman K), so why did
I need to add the escape character to get it to work?
2) If the expression evaluates to true/false for the purposes of the if
statement, why does it not return true/false reliably in the shortened form?

TIA
Regards
Paul
 
L

Lasse Reichstein Nielsen

Paul Lautman said:
if (val.match(/^s+$/) || val == "") ....
When I tried it it didn't work, but I found that I could make it work by
changing the regex to /^\s+$/
1) I'm sure that the function worked for the author (Nariman K), so why did
I need to add the escape character to get it to work?

Don't be too sure. It *is* wrong. The /^s+$/ regexp matches a string
consisting of one or more "s"'s. The regexp /^\s+$/ matches a string
of one or more whitespace characters.
2) If the expression evaluates to true/false for the purposes of the if
statement, why does it not return true/false reliably in the shortened form?

It should, if it worked. It seems you removed the backslash again.

Code like:
if (..condition..) {
return true;
} else {
return false;
}
is a sign of someone either not understanding the language very well,
or someone having a personally preferred style that I cannot agree with :).

In your case, this should suffice:

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

The idea of a form validation object is not bad, but some of the
coding style of the article you refer to could be better, e.g.,
creating a lot of global functions and then assigning them to the
object in the constructor, instead of assigning them directly to the
prototype object (and the e-mail address validator is overly
restrictive, although not as bad as some I have seen).

Good luck
/L
 
R

RobG

Paul said:
Hi y'all,
I found this function at
http://www.devshed.com/c/a/JavaScript/Form-Validation-with-JavaScript/6/

// check to see if input is whitespace only or empty
function isEmpty(val)
{
if (val.match(/^s+$/) || val == "")

That regular expression will match a string consisting only of one or
more 's' characters. It will return an array object of all the matches.

Otherwise, it will return null (which type-converts to boolean false).

If there are no characters at all, val=='' will return true.

So if you enter only one or more 's' characters or nothing at all, the
if expression returns true.


[...]
When I tried it it didn't work, but I found that I could make it work by
changing the regex to /^\s+$/

That will match a string consisting of one or more whitespace characters
only (space, tab, and so on). The returned object type-converts to 'true'.

I then tried shortening it to:

function isEmpty(val)
{
return (val.match(/^s+$/) || val == "");

Now you are back to square one.

[...]
Can someone answer the following 2 questions for me:

1) I'm sure that the function worked for the author (Nariman K), so why did
I need to add the escape character to get it to work?

Can't answer that, it should 'work' only as described above.

2) If the expression evaluates to true/false for the purposes of the if
statement, why does it not return true/false reliably in the shortened form?

It doesn't do what you think it's doing. You probably want to see if
the value is nothing or only whitespace, in which case you want to test
if it contains any non-whitespace character:

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

Note the additional backslash '\' before the 'S' character to let the
regular expression know to use the special meaning of 'S' to match any
non-whitespace character.

Incidentally, test() returns true or false which seems more appropriate
for this test as no type-conversion is required.
 
P

Paul Lautman

What I actually had was:

function isEmpty(val)
{
return (val.match(/^\s+$/) || val == '')
}
and this returned the value matched (when it is spaces).

But the two examples using the test() method are exactly what I was after.
Thanks Lasse and Rob.
 
T

Thomas 'PointedEars' Lahn

RobG said:
[...] You probably want to see if the value is nothing or only
whitespace, in which case you want to test if it contains any
non-whitespace character:

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

Note the additional backslash '\' before the 'S' character to let the
regular expression know to use the special meaning of 'S' to match any
non-whitespace character.

Therefore the method will return `false' if `val' is "empty", contrary to
the meaning attached to it by its identifier. Probably you wanted to

return !/\S/.test(val);
Incidentally, test() returns true or false which seems more appropriate
for this test as no type-conversion is required.

.... iff the return value is negated ;-)


PointedEars
 
R

RobG

Thomas said:
RobG said:
[...] You probably want to see if the value is nothing or only
whitespace, in which case you want to test if it contains any
non-whitespace character:

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

Note the additional backslash '\' before the 'S' character to let the
regular expression know to use the special meaning of 'S' to match any
non-whitespace character.

Therefore the method will return `false' if `val' is "empty", contrary to
the meaning attached to it by its identifier. Probably you wanted to

return !/\S/.test(val);
Incidentally, test() returns true or false which seems more appropriate
for this test as no type-conversion is required.

... iff the return value is negated ;-)

Or the function is re-named 'isNotEmpty()' :)
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,010
Latest member
MerrillEic

Latest Threads

Top