need: eagle eye to check form validation!

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

    #1

    need: eagle eye to check form validation!

    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="retur n
    checkFields();"


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

    <SCRIPT language="JavaS cript">

    <!-- 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.valu e == "") {
    missinginfo += "\n - URL of your LINKS PAGE";
    }

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

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

    if(document.for m1.Description. value == "") {
    missinginfo += "\n - Description of your Site";
    }
    if ((document.form 1.email.value == "") ||
    (document.form1 .email.value.in dexOf('@') == -1) ||
    (document.form1 .email.value.in dexOf('.') == -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(missingin fo);
    return false;
    }
    else return true;
    }
    // End -->
    </script>
    =============== =======END SCRIPT========= ==========


  • Vincent van Beveren

    #2
    Re: need: eagle eye to check form validation!

    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:
    [color=blue]
    > 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="retur n
    > checkFields();"
    >
    >[/color]

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: need: eagle eye to check form validation!

      "Axel Foley" <devmoaner@sync hkat.com> writes:
      [color=blue]
      > 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.[/color]

      "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.
      [color=blue]
      > If anyone would be so kind as to comb it for apparent flaws,[/color]

      Flaws ... My pleasure! Actual errors might also be discovered if any
      exists.
      [color=blue]
      > My <form> statement includes the following:... onSubmit="retur n
      > checkFields();"[/color]

      That looks fine.
      [color=blue]
      > =============== ==BEGIN SCRIPT========= =====
      >
      > <SCRIPT language="JavaS cript">[/color]

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

      Using HTML comments inside a Javascript instead of propert Javascript
      comments: /* lala */
      [color=blue]
      > <!-- Begin[/color]

      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.
      [color=blue]
      > function checkFields() {
      > missinginfo = "";[/color]

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

      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 == "") {
      ....[color=blue]
      > missinginfo += "\n - Name";
      > }
      >
      > if (document.form1 .website.value == "") {[/color]

      .... and
      if (document.forms['form1'].elements['website'].value == "") {
      ....
      [color=blue]
      > missinginfo += "\n - Website Name";
      > }
      >
      >
      > if (document.form1 .links_URL.valu e == "") {[/color]

      .... 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 == "") {
      [color=blue]
      > missinginfo += "\n - URL of your LINKS PAGE";
      > }
      >
      > if ((document.form 1.URL.value == "") ||
      > (document.form1 .URL.value.inde xOf("http://") == -1) ||[/color]

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

      [color=blue]
      > if (missinginfo != "") {
      > missinginfo ="_____________ _______________ _\n" +
      > "You failed to correctly fill in your:\n" +
      > missinginfo + "\n____________ _______________ __" +
      > "\nPlease re-enter and submit again!";[/color]

      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.

      [color=blue]
      > else return true;[/color]

      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 - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      Working...