Trying to Write a Generic Form Validator

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

    Trying to Write a Generic Form Validator

    I'm trying to write a generic/reusable form validator in Javascript...
    just something that checks to make sure required fields have a value. By
    generic I mean I don't want to explicitly reference the name/id of the
    form or the name of any of the data fields within a "validation "
    function.

    My first shot seems to have some errors in it:

    FieldsToValidat eByForm = {};
    FieldsToValidat eByForm['contact'] = ["FirstName" ,
    "LastName","Sta te","Email"];

    function validate(form)
    {
    problemFields = new Array();
    returnval = true;
    FieldsToValidat e = FieldsToValidat eByForm[form.id];

    for(i=0; i < FieldsToValidat e.length; i++) {
    fieldInQuestion = form[FieldsToValidat e[i]];
    if(fieldInQuest ion.value.lengt h < 1) //problem spot?
    problemFields.p ush(FieldsToVal idate[i]);
    }

    if(problemField s.length > 0) {
    returnval = false;
    warn(problemFie lds); /* tells user they're missing a field,
    that's all */
    }

    return returnval;
    }


    What I think is happening (not sure) is that the expression
    form[fieldsToValidat e[i]] is not giving me what I want: a reference to
    the object corresponding to the form field with the same name. In
    otherwords, I must have some fundamental misunderstandin g of how the DOM
    works here. Unfortunately, I can't seem to find a good enough reference
    to set me straight....

    -W

    ~==~

    Taking Pictures During Dreams
    weston8[at]cann8central.or g
    (remove eights to email me)

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Bagbourne

    #2
    Re: Trying to Write a Generic Form Validator

    "Weston C" <west8on[at]cann8central.Re moveEights.org> wrote in message
    news:3efce361$0 $200$75868355@n ews.frii.net...[color=blue]
    > I'm trying to write a generic/reusable form validator in Javascript...
    > just something that checks to make sure required fields have a value. By
    > generic I mean I don't want to explicitly reference the name/id of the
    > form or the name of any of the data fields within a "validation "
    > function.[/color]
    [color=blue]
    > What I think is happening (not sure) is that the expression
    > form[fieldsToValidat e[i]] is not giving me what I want: a reference to
    > the object corresponding to the form field with the same name. In
    > otherwords, I must have some fundamental misunderstandin g of how the DOM
    > works here. Unfortunately, I can't seem to find a good enough reference
    > to set me straight....[/color]

    Try this:

    ng.html (I have this saved locally because it's so useful!)

    In particular the "Object HTMLDocument" section, and its "forms" property
    which is an HTMLCollection (which is also fully documented there)

    You should be able to follow the DOM through and figure it out from there...

    Nige


    Comment

    • John

      #3
      Re: Trying to Write a Generic Form Validator

      Weston C <west8on[at]cann8central.Re moveEights.org> wrote in message news:<3efce361$ 0$200$75868355@ news.frii.net>. ..[color=blue]
      > I'm trying to write a generic/reusable form validator in Javascript...
      > just something that checks to make sure required fields have a value. By
      > generic I mean I don't want to explicitly reference the name/id of the
      > form or the name of any of the data fields within a "validation "
      > function.
      >
      > My first shot seems to have some errors in it:
      >
      > FieldsToValidat eByForm = {};
      > FieldsToValidat eByForm['contact'] = ["FirstName" ,
      > "LastName","Sta te","Email"];
      >
      > function validate(form)
      > {
      > problemFields = new Array();
      > returnval = true;
      > FieldsToValidat e = FieldsToValidat eByForm[form.id];
      >
      > for(i=0; i < FieldsToValidat e.length; i++) {
      > fieldInQuestion = form[FieldsToValidat e[i]];
      > if(fieldInQuest ion.value.lengt h < 1) //problem spot?
      > problemFields.p ush(FieldsToVal idate[i]);
      > }
      >
      > if(problemField s.length > 0) {
      > returnval = false;
      > warn(problemFie lds); /* tells user they're missing a field,
      > that's all */
      > }
      >
      > return returnval;
      > }
      >
      >
      > What I think is happening (not sure) is that the expression
      > form[fieldsToValidat e[i]] is not giving me what I want: a reference to
      > the object corresponding to the form field with the same name. In
      > otherwords, I must have some fundamental misunderstandin g of how the DOM
      > works here. Unfortunately, I can't seem to find a good enough reference
      > to set me straight....
      >
      > -W
      >
      > ~==~
      > http://weston.canncentral.org/
      > Taking Pictures During Dreams
      > weston8[at]cann8central.or g
      > (remove eights to email me)
      >
      > *** Sent via Developersdex http://www.developersdex.com ***
      > Don't just participate in USENET...get rewarded for it![/color]

      Many ways.

      This is available easily from the form object. DOM is just a
      distraction.

      I strobe through the form elements, inspecting type for how to handle
      the object value. For example, a "select-one" <select> object has a
      ..selectedIndex property, so obj[obj.selectedInd ex].value would index a
      value. .checked for others....
      Something I noticed of ie long ago is that it can lose the form object
      reference when being handed another function deeper. So I always
      reconstruct the reference within the next function by the sloppy: var
      oForm = eval( document[oForm.name] ), which requires the form at least
      be named, or use the getElementByNam e/ID.
      One could have the onBlur/onChange inspect the form object element
      upon use of that element by the user. If empty or an off checkbox,
      then set a global JS flag, maybe bSubmitForm = false.

      Comment

      • Richard Cornford

        #4
        Re: Trying to Write a Generic Form Validator

        "John" <jhillyer2@hotm ail.com> wrote in message
        news:6087c78e.0 306290251.12c05 cd0@posting.goo gle.com...
        <snip>[color=blue]
        > Something I noticed of ie long ago is that it can lose the
        >form object reference when being handed another function deeper.
        >So I always reconstruct the reference within the next function
        >by the sloppy: var oForm = eval( document[oForm.name] ), which
        >requires the form at least be named,....[/color]
        <snip>

        Whatever you think might be justifying this operation you are almost
        certainly utterly mistaken. In order for this operation to return a
        reference to a named form and assign it to the - oForm - variable -
        oForm - must start off holding a reference to that form, else the name
        will not resolve. That makes the whole operation pointless to start
        with. The additional use of the - eval - function demonstrates a
        complete lack of understanding of the actions of that function and is
        completely futile.

        It would be better to understand the actions of the - eval - function
        before recommending its use to anyone but, as the use of - eval - is
        almost never necessary and its appearance in JavaScript source code is
        usually indicative of an ill-conceived approach, it would be better to
        never recommend the use of - eval - at all.

        Richard.

        --

        Example JavaScript DOM listings for: Opera 7.11,
        Mozilla 1.2 and ICEbrowser 5.4
        <URL: http://www.litotes.demon.co.uk/dom_root.html >


        Comment

        • JavaScript-Coder.com

          #5
          Re: Trying to Write a Generic Form Validator

          Checkout the form validation script located at:

          It is a general form validation script.
          You can use the script for most of the validations.

          Hope this helps..

          Prasanth,

          Javascript Coder is a tool that can generate
          code for a number of cool JavaScript Features.



          Weston C <west8on[at]cann8central.Re moveEights.org> wrote in message news:<3efce361$ 0$200$75868355@ news.frii.net>. ..[color=blue]
          > I'm trying to write a generic/reusable form validator in Javascript...
          > just something that checks to make sure required fields have a value. By
          > generic I mean I don't want to explicitly reference the name/id of the
          > form or the name of any of the data fields within a "validation "
          > function.
          >
          > My first shot seems to have some errors in it:
          >
          > FieldsToValidat eByForm = {};
          > FieldsToValidat eByForm['contact'] = ["FirstName" ,
          > "LastName","Sta te","Email"];
          >
          > function validate(form)
          > {
          > problemFields = new Array();
          > returnval = true;
          > FieldsToValidat e = FieldsToValidat eByForm[form.id];
          >
          > for(i=0; i < FieldsToValidat e.length; i++) {
          > fieldInQuestion = form[FieldsToValidat e[i]];
          > if(fieldInQuest ion.value.lengt h < 1) //problem spot?
          > problemFields.p ush(FieldsToVal idate[i]);
          > }
          >
          > if(problemField s.length > 0) {
          > returnval = false;
          > warn(problemFie lds); /* tells user they're missing a field,
          > that's all */
          > }
          >
          > return returnval;
          > }
          >
          >
          > What I think is happening (not sure) is that the expression
          > form[fieldsToValidat e[i]] is not giving me what I want: a reference to
          > the object corresponding to the form field with the same name. In
          > otherwords, I must have some fundamental misunderstandin g of how the DOM
          > works here. Unfortunately, I can't seem to find a good enough reference
          > to set me straight....
          >
          > -W
          >
          > ~==~
          > http://weston.canncentral.org/
          > Taking Pictures During Dreams
          > weston8[at]cann8central.or g
          > (remove eights to email me)
          >
          > *** Sent via Developersdex http://www.developersdex.com ***
          > Don't just participate in USENET...get rewarded for it![/color]

          Comment

          • Mark Szlazak

            #6
            Re: Trying to Write a Generic Form Validator

            The chapter on form validation from Danny Goodmans "Javascript and DHTML
            Cookbook" are online at:

            Part 1: http://www.webreference.com/programm...dhtml/chap8/1/

            Part 2:
            Learn the basics of Java, including its history, features, syntax, and use cases to enrich your knowledge base for building scalable web applications.



            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            Working...