Test on <input> value fails

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

    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 08:07:10 -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

    • Alvaro G. Vicario

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

      *** Don escribió/wrote (Thu, 03 Feb 2005 13:10:04 -0700):[color=blue]
      > Never mind. I found my problem. Can't believe I did this. Wasn't using
      > the correct <form> name on the second validation pass.[/color]

      Glad to hear that.

      BTW, JavaScripts supports regular expressions. You can probably replace
      your 14-line functions with one-liners similar to:

      return text.replace(/\s+)/g, '')==''; // Not tested


      --
      -+ Álvaro G. Vicario - Burgos, Spain
      +- http://www.demogracia.com (la web de humor barnizada para la intemperie)
      ++ Manda tus dudas al grupo, no a mi buzón
      -+ Send your questions to the group, not to my mailbox
      --

      Comment

      Working...