Empty input box

Discussion in 'Javascript' started by Paul Lautman, Apr 24, 2006.

  1. Paul Lautman

    Paul Lautman Guest

    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
     
    Paul Lautman, Apr 24, 2006
    #1
    1. Advertising

  2. "Paul Lautman" <> writes:

    > 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
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Apr 24, 2006
    #2
    1. Advertising

  3. Paul Lautman

    RobG Guest

    Paul Lautman wrote:
    > 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.


    --
    Rob
     
    RobG, Apr 24, 2006
    #3
  4. Paul Lautman

    Paul Lautman Guest

    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.

    Paul Lautman wrote:
    > 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
     
    Paul Lautman, Apr 24, 2006
    #4
  5. RobG wrote:

    > [...] 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
    --
    Multiple exclamation marks are a sure sign of a diseased mind.
    -- Terry Pratchett
     
    Thomas 'PointedEars' Lahn, Apr 24, 2006
    #5
  6. Paul Lautman

    RobG Guest

    Thomas 'PointedEars' Lahn wrote:
    > RobG wrote:
    >
    >> [...] 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()' :)


    --
    Rob
     
    RobG, Apr 25, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ashutosh
    Replies:
    3
    Views:
    9,001
    Andrew Thompson
    Jun 16, 2005
  2. cjl
    Replies:
    1
    Views:
    10,503
    Michael Winter
    Oct 31, 2005
  3. Lukas
    Replies:
    3
    Views:
    814
    spiff
    Nov 10, 2005
  4. chirs

    How to test an empty input box?

    chirs, Oct 17, 2003, in forum: Javascript
    Replies:
    4
    Views:
    97
    Richard Hockey
    Oct 18, 2003
  5. cjl
    Replies:
    7
    Views:
    182
    Thomas 'PointedEars' Lahn
    Oct 30, 2005
Loading...

Share This Page