Connecting Tech Pros Worldwide Forums | Help | Site Map

Test on <input> value fails

Don
Guest
 
Posts: n/a
#1: Jul 17 '05
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.indexOf(c) == -1)
return false;
}
// All characters are whitespace.
return true;
}
//
//
function validate()
{
if(isEmpty(document.online_registration_form_calc. names.value))
{
alert('Entry in "Name\(s\)" field required.');
document.online_registration_form_calc.names.focus ();
return false;
}
return true;
}


<?php
print("<input name=\"names\" type=\"text\" id=\"names\" value=\"".$_REQUEST['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
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Test on <input> 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.indexOf(c) == -1)
> return false;
> }
> // All characters are whitespace.
> return true;
> }
>//
>//
> function validate()
> {
> if(isEmpty(document.online_registration_form_calc. names.value))
> {
> alert('Entry in "Name\(s\)" field required.');
> document.online_registration_form_calc.names.focus ();
> return false;
> }
> return true;
> }
>
>
><?php
> print("<input name=\"names\" type=\"text\" id=\"names\" value=\"".$_REQUEST['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 =----
Alvaro G. Vicario
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Test on <input> 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
--
Closed Thread