Test on <input> value fails

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Don

    #1

    Test on <input> value fails

    Can't figure this one out. The "validate" function below checks to make sure the field "names" is
    not null. "validate" actually calls "isEmpty", which scans the field.

    Here's the situation. <input> for "names" first existed in an html file. The test worked just fine
    there. If the field was filled, the test would pass. If the field was cleared after having been
    filled, it would fail as it should.

    The field is then passed, via POST, to a PHP script, where it is checked for non-null in the same
    manner using the code below. The same field content appears when the page is returned to the
    client. The only thing is if I clear the field, the JavaScrpt function "validate" thinks the field
    is still non-null, and the test passes when it should have failed. Any idea what's going on here?

    Thanks for your help,
    Don


    //
    // Define whitespace characters
    var whitespace = " \t\n\r";
    function isEmpty(s)
    {
    var i;
    if((s==null) || (s.length == 0))
    return true;
    // Search string looking for characters that are not whitespace
    for(i=0; i <s.length; i++)
    {
    var c = s.charAt(i);
    if(whitespace.i ndexOf(c) == -1)
    return false;
    }
    // All characters are whitespace.
    return true;
    }
    //
    //
    function validate()
    {
    if(isEmpty(docu ment.online_reg istration_form_ calc.names.valu e))
    {
    alert('Entry in "Name\(s\)" field required.');
    document.online _registration_f orm_calc.names. focus();
    return false;
    }
    return true;
    }


    <?php
    print("<input name=\"names\" type=\"text\" id=\"names\" value=\"".$_REQ UEST['names']."\"
    size=\"97\">\n" );
    ?>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
  • Don

    #2
    Re: Test on &lt;input&gt ; value fails

    On Thu, 03 Feb 2005 07:23:21 -0700, Don <no@adr.com> wrote:
    [color=blue]
    >Can't figure this one out. The "validate" function below checks to make sure the field "names" is
    >not null. "validate" actually calls "isEmpty", which scans the field.
    >
    >Here's the situation. <input> for "names" first existed in an html file. The test worked just fine
    >there. If the field was filled, the test would pass. If the field was cleared after having been
    >filled, it would fail as it should.
    >
    >The field is then passed, via POST, to a PHP script, where it is checked for non-null in the same
    >manner using the code below. The same field content appears when the page is returned to the
    >client. The only thing is if I clear the field, the JavaScrpt function "validate" thinks the field
    >is still non-null, and the test passes when it should have failed. Any idea what's going on here?
    >
    >Thanks for your help,
    >Don
    >
    >
    >//
    >// Define whitespace characters
    > var whitespace = " \t\n\r";
    > function isEmpty(s)
    > {
    > var i;
    > if((s==null) || (s.length == 0))
    > return true;
    > // Search string looking for characters that are not whitespace
    > for(i=0; i <s.length; i++)
    > {
    > var c = s.charAt(i);
    > if(whitespace.i ndexOf(c) == -1)
    > return false;
    > }
    > // All characters are whitespace.
    > return true;
    > }
    >//
    >//
    > function validate()
    > {
    > if(isEmpty(docu ment.online_reg istration_form_ calc.names.valu e))
    > {
    > alert('Entry in "Name\(s\)" field required.');
    > document.online _registration_f orm_calc.names. focus();
    > return false;
    > }
    > return true;
    > }
    >
    >
    ><?php
    > print("<input name=\"names\" type=\"text\" id=\"names\" value=\"".$_REQ UEST['names']."\"
    >size=\"97\">\n ");
    >?>
    >
    >----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    >http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    >----= East and West-Coast Server Farms - Total Privacy via Encryption =----[/color]
    Never mind. I found my problem. Can't believe I did this. Wasn't using the correct <form> name on
    the second validation pass.

    Thanks anyway.
    Don

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

    Comment

    • Dr John Stockton

      #3
      Re: Test on &lt;input&gt ; value fails

      JRS: In article <0ub401dg7eahpv skf3eci4a9u7llb 4q946@4ax.com>, dated
      Thu, 3 Feb 2005 07:23:21, seen in news:comp.lang. javascript, Don
      <no@adr.com> posted :
      [color=blue]
      >// Define whitespace characters
      > var whitespace = " \t\n\r";
      > function isEmpty(s)
      > {
      > var i;
      > if((s==null) || (s.length == 0))
      > return true;
      > // Search string looking for characters that are not whitespace
      > for(i=0; i <s.length; i++)
      > {
      > var c = s.charAt(i);
      > if(whitespace.i ndexOf(c) == -1)
      > return false;
      > }
      > // All characters are whitespace.
      > return true;
      > }[/color]

      That looks rather like

      function isEmpty(s) { return !/\S/.test(s) }

      but perhaps you are paid by the yard of code?

      <URL:http://www.merlyn.demo n.co.uk/js-valid.htm>

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

      Comment

      Working...