Re: Empty input box



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
.



Relevant Pages

  • Re: String trim (was JavaScript Functions)
    ... By "work" I assume you mean that it corrects the fact that some javascript engines do not match '\u00A0' when \s is used in a regular expression. ... But is this an attempt to create methods that conform to the ECMA specification, and thus are consistent across platforms, or is it a desire to arbitrarily include the non-breaking space in the set of matched characters for a trim function. ... The ECMA spec wants \s to match javascript's whit space characters and it line terminator characters, but the definition of whitespace includes all of Unicode's Zs group, and JScript, for example, does not match the majority of those. ... var LineTerminator = [ ...
    (comp.lang.javascript)
  • Re: Regular Expression Function
    ... I want a regular expression to compare sentences and then rate them as ... I have an array with a list of other phrases like so... ... characters will throw things off. ... "In an hour the system will go down for maintenance". ...
    (alt.php)
  • Re: Regular Expression Function
    ... I want a regular expression to compare sentences and then rate them as ... I have an array with a list of other phrases like so.. ... These will be stripped from the input first. ... characters will throw things off. ...
    (alt.php)
  • Re: Expert script (.bat) writers help needed (strip double-quote from string)
    ... Sets or returns the regular expression pattern being searched for. ... Always a RegExp object variable. ... May include any of the regular expression characters defined in the table in the Settings section. ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: RegExp and special caracters
    ... function identifier(str, substr) ... Expression syntax called POSIX 1003.2 Basic Regular Expressions (considered ... where special characters are to be escaped in order not to be considered ... Expressions modelled after the Regular Expression flavor Perl 5 supports ...
    (comp.lang.javascript)