need: eagle eye to check form validation!

Discussion in 'Javascript' started by Axel Foley, Jun 25, 2004.

  1. Axel Foley

    Axel Foley Guest

    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===================
     
    Axel Foley, Jun 25, 2004
    #1
    1. Advertising

  2. 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

    Axel Foley wrote:

    > 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();"
    >
    >
     
    Vincent van Beveren, Jun 25, 2004
    #2
    1. Advertising

  3. "Axel Foley" <> writes:

    > 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:
    <script type="text/javascript">

    > <!-- 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
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jun 25, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. (=?iso-8859-1?Q?=AF=60=B7=2E=2E=2E=F8=A4=B0=60=B0=

    EAGLE v4.11 Professional *Bilingual* - Cadsoft (Windows, Linux - new !

    (=?iso-8859-1?Q?=AF=60=B7=2E=2E=2E=F8=A4=B0=60=B0=, Oct 7, 2003, in forum: VHDL
    Replies:
    0
    Views:
    823
    (=?iso-8859-1?Q?=AF=60=B7=2E=2E=2E=F8=A4=B0=60=B0=
    Oct 7, 2003
  2. haim
    Replies:
    0
    Views:
    501
  3. Frank
    Replies:
    1
    Views:
    379
    Aleksander Straczek
    Nov 22, 2004
  4. Peter Reinde

    Red eye correction

    Peter Reinde, Jan 4, 2004, in forum: C++
    Replies:
    6
    Views:
    534
    Jon Bell
    Jan 4, 2004
  5. Fish Eye processing

    , May 31, 2007, in forum: C Programming
    Replies:
    1
    Views:
    357
    Keith Thompson
    Jun 1, 2007
Loading...

Share This Page