need: eagle eye to check form validation!

A

Axel Foley

Hi Folks,

i'm newbie at JS; but "learning by tweaking" is my middle name!

Trying to set up a link partnership application on a client's site; got this
script at "The Javascript Source", but it does not work for me.

If anyone would be so kind as to comb it for apparent flaws, I would be
greatly indebted!
My <form> statement includes the following:... onSubmit="return
checkFields();"


=================BEGIN SCRIPT==============

<SCRIPT language="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function checkFields() {
missinginfo = "";
if (document.form1.name.value == "") {
missinginfo += "\n - Name";
}

if (document.form1.website.value == "") {
missinginfo += "\n - Website Name";
}


if (document.form1.links_URL.value == "") {
missinginfo += "\n - URL of your LINKS PAGE";
}

if ((document.form1.URL.value == "") ||
(document.form1.URL.value.indexOf("http://") == -1) ||
(document.form1.URL.value.indexOf(".") == -1)) {
missinginfo += "\n - URL of your Web site";
}

if ((document.form1.links_URL.value == "") ||
(document.form1.links_URL.value.indexOf("http://") == -1) ||
(document.form1.links_URL.value.indexOf(".") == -1)) {
missinginfo += "\n - URL of your LINKS PAGE";
}

if(document.form1.Description.value == "") {
missinginfo += "\n - Description of your Site";
}
if ((document.form1.email.value == "") ||
(document.form1.email.value.indexOf('@') == -1) ||
(document.form1.email.value.indexOf('.') == -1)) {
missinginfo += "\n - Email Address";
}


if (missinginfo != "") {
missinginfo ="_____________________________\n" +
"You failed to correctly fill in your:\n" +
missinginfo + "\n_____________________________" +
"\nPlease re-enter and submit again!";
alert(missinginfo);
return false;
}
else return true;
}
// End -->
</script>
======================END SCRIPT===================
 
V

Vincent van Beveren

If you can give us the URL it would help debugging. The script looks
fine to me. Important is to note that JavaScript is casesensetive. Maybe
'description' should be all small caps or so.

Vincent
 
L

Lasse Reichstein Nielsen

Axel Foley said:
i'm newbie at JS; but "learning by tweaking" is my middle name!

Trying to set up a link partnership application on a client's site; got this
script at "The Javascript Source", but it does not work for me.

"Does not work" is not a very good bug report. It's actually about the
worst that still qualify as reporting a bug.

To report a bug, you should give enough information for us to:
1: reproduce the bug. That is, show the entire page that exhibits the
bug, as well as specify the browser and other runtime environment
details.
Instead of posting an entire page, it's best to first reduce the
page to a small self-contained example that still exhibits the bug.
In many cases, that process will let you discover the bug yourself.
2: recognize the bug. We can run the page all day, but unless the bug
is as blatant as a syntax error, it's likely that we won't know
correct behavior from incorrect, because you haven't described
the correct behavior.
3: repair the bug. Again, we need to know what the correct behavior
is in order to change the program to achieve it.
If anyone would be so kind as to comb it for apparent flaws,

Flaws ... My pleasure! Actual errors might also be discovered if any
exists.
My <form> statement includes the following:... onSubmit="return
checkFields();"

That looks fine.
=================BEGIN SCRIPT==============

<SCRIPT language="JavaScript">

The "type" attribute is required in HTML 4, and is always suffient.
Use:
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

Using HTML comments inside a Javascript instead of propert Javascript
comments: /* lala */
<!-- Begin

Attempting to "hide" the script from "old" browsers is not necessary.
Old browsers means Netscape 1 and contemporaries. They are no longer
in use, or if they are, the entire page will most likely fail blatantly.
function checkFields() {
missinginfo = "";

Local variables should be declared as such. This creates a global
variable, polluting the global namespace. Use:
var missinginfo = "";
if (document.form1.name.value == "") {

There are pages where this will fail, although it works in most
browsers on most (non XHTML) pages. To be safe, I recommend using:
if (document.forms['form1'].elements['name'].value == "") {
....
missinginfo += "\n - Name";
}

if (document.form1.website.value == "") {

.... and
if (document.forms['form1'].elements['website'].value == "") {
....
missinginfo += "\n - Website Name";
}


if (document.form1.links_URL.value == "") {

.... and ... you get the point.

It's probably prudent to make a shortcut to the form's elements.
Start the function with:
var form = document.forms['form1'].elements;
and then use, e.g.,
if (form['link_URL'].value == "") {
missinginfo += "\n - URL of your LINKS PAGE";
}

if ((document.form1.URL.value == "") ||
(document.form1.URL.value.indexOf("http://") == -1) ||

I would want the protocol to come first, so instead of "== -1", I would
do "!= 0"

if (missinginfo != "") {
missinginfo ="_____________________________\n" +
"You failed to correctly fill in your:\n" +
missinginfo + "\n_____________________________" +
"\nPlease re-enter and submit again!";

Since you cannot predict the font used by the alert dialog, using
a fixed number of underscores can give widely different visual
results on different pages.

else return true;

I recommend putting { and } around all if/else blocks, even when
not necessitated by the syntax. It eases reading profoundly.

else { return true; }



I see no errors in the script. If it fails to do as you expect, the
problem is either in your expectations or in the remainder of the
page. Is the name of the form really "form1"? As in:
<form id="form1" ...>
or
<form name="form1" ...>

Are the name of the mentioned form controls also correct?

/L
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top