Form Validation

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

    #1

    Form Validation

    I have a form that has 10 fields on it. I have made all of them "Required".
    I also am using vb if statements to decide whether or not each field should
    be on the page. I am using the vb to compare values in my database and if a
    certain field equals something then the field is shown on the form. When
    the form is displayed in the browser, it could any increment of the 10
    fields on it. My problem is that unless all fields are being shown then the
    validation does not work and the form submits even with blank fields. Has
    anyone tackled this kind of a situation before and if so how did they
    accomplish it? Thanks in advance for your help!

    Darren


  • DJ Craig

    #2
    Re: Form Validation

    Use Javascript, it's better than VBScript.
    Or go to alt.comp.lang.v bscript

    Comment

    • Darren

      #3
      Re: Form Validation


      "DJ Craig" <spit@djtriciti es.com> wrote in message
      news:1116955383 .938508.23190@g 43g2000cwa.goog legroups.com...[color=blue]
      > Use Javascript, it's better than VBScript.
      > Or go to alt.comp.lang.v bscript
      >[/color]

      I think that I explained this wrong in my first post. I am using javascript
      to validate the forms and asp if statements to decide which form show on the
      page. The problem it that the javascript is not working unless all of the
      form fields are visible on the page.


      Comment

      • kaeli

        #4
        Re: Form Validation

        In article <1d354$42935aca $d8cc6cea$27572 @LIGHTSHIP.NET> , news@lan-
        specialist.com enlightened us with...[color=blue]
        > I have a form that has 10 fields on it. I have made all of them "Required".[/color]

        Meaning what?
        ASP.NET?
        [color=blue]
        > I also am using vb if statements to decide whether or not each field should
        > be on the page.[/color]

        The browser doesn't see server-side code.
        [color=blue]
        > I am using the vb to compare values in my database and if a
        > certain field equals something then the field is shown on the form. When
        > the form is displayed in the browser, it could any increment of the 10
        > fields on it. My problem is that unless all fields are being shown then the
        > validation does not work and the form submits even with blank fields. Has
        > anyone tackled this kind of a situation before and if so how did they
        > accomplish it? Thanks in advance for your help![/color]

        Which runs on the server, and which the client?
        This group is for javascript. If the problem is with the VB on the server,
        posting here doesn't help much unless one of us who knows VB decides to go
        ahead and answer an OT question.

        Post your .NET code and I'll check it out.
        Providing a URL so I can see what the browser gets is also helpful.

        --
        --
        ~kaeli~
        Contrary to popular opinion, the plural of 'anecdote' is
        not 'fact'.



        Comment

        • Random

          #5
          Re: Form Validation

          Without a little more information, it's hard to understand where your
          problem might be.

          Here's my understanding:
          Your ASP is conditionally outputting form fields. In many cases, a form
          field simply won't exist in the HTML that is being output.

          Your JavaScript form validation is broken-- it will only validate
          properly if ALL fields exist on the page, which is rarely the case.


          If this is correct, you need to add to your validation a check to see
          if the element exists. If it does, validate its value. If it does not,
          skip it and/or consider it valid.

          This check is fairly simple since JavaScript is so tolerant. If an
          element exists, it evaluates to true. If it does not, it will not.

          if( document.forms[ myForm ].elements[ myElement ] )
          validate( myElement );



          Darren wrote:[color=blue]
          > "DJ Craig" <spit@djtriciti es.com> wrote in message
          > news:1116955383 .938508.23190@g 43g2000cwa.goog legroups.com...[color=green]
          > > Use Javascript, it's better than VBScript.
          > > Or go to alt.comp.lang.v bscript
          > >[/color]
          >
          > I think that I explained this wrong in my first post. I am using javascript
          > to validate the forms and asp if statements to decide which form show on the
          > page. The problem it that the javascript is not working unless all of the
          > form fields are visible on the page.[/color]

          Comment

          • RobG

            #6
            Re: Form Validation

            Darren wrote:[color=blue]
            > "DJ Craig" <spit@djtriciti es.com> wrote in message
            > news:1116955383 .938508.23190@g 43g2000cwa.goog legroups.com...
            >[color=green]
            >>Use Javascript, it's better than VBScript.
            >>Or go to alt.comp.lang.v bscript
            >>[/color]
            >
            >
            > I think that I explained this wrong in my first post. I am using javascript
            > to validate the forms and asp if statements to decide which form show on the
            > page. The problem it that the javascript is not working unless all of the
            > form fields are visible on the page.
            >
            >[/color]

            Form validation is usually done from the form onsubmit event:

            <form ... onsubmit="retur n validateForm(th is);" ...>

            The validateForm() function tests whatever, then returns true or false
            depending on the success or failure of the validation. If false is
            returned, the form won't be submitted (at least, that's the theory).

            Please remember that the fact that your forum subsequently submits does
            not mean it actually passed validation, you can't guarantee that at
            all. The user may have JavaScript disabled or may have bypassed your
            validation routine some other way. Check everything again on the
            server.

            Your validateForm() function may go something like:

            function validateForm(f) {
            var formElements = f.elements;
            var formValid = true;
            var i = 0;
            if ( formElements[i] ){
            var el = formElements[i];
            do {
            if ( 'someElementNam e' == el.name ) {
            /* do validation for this element, if it fails:
            *
            * formValid = false;
            *
            */
            }
            if ( 'anotherElement Name' == el.name ) {
            /* do validation for this element, if it fails:
            *
            * formValid = false;
            *
            */
            }
            } while ( (el = formElements[++i]) )
            }
            return formValid;
            }

            You may wish to use 'switch' (JavaScript's version of 'case') instead
            of if's, that's up to you. It is also handy to base your 'if' on the
            className, say if you have lots of text fields that you just want to
            check that they have something in them:

            if ( 'plainText' == el.className && '' == el.value ) {
            formValid = false;
            }

            In your HTML:

            <input type="text" class="plainTex t" ... >

            The className 'plainText' doesn't have to be defined anywhere, just set
            on the element. You can cope with multiple class names too, it just
            takes a little more effort (and is a tad slower also):

            if ( /\bplainText\b/.test(el.classN ame) && '' == el.value ) {
            formValid = false;
            }


            Have fun.

            --
            Rob

            Comment

            • Danny

              #7
              Re: Form Validation


              Yes, VBS is only for Internet Explorer browser and in windows 32bit
              platforms, so, won't work in say macOS IE, whereas javascript will do fine
              in any browser/platform. What you need is select this or that element and
              then more options are shown, that were not there Unless you do
              choose/select/tick/check/uncheck/else that element, very easily done in js
              by using the objects style stack, if (SOANDSOHAPPENS )
              { obj.style.displ ay='none'} else { obj.style.displ ay='inline' /// or
              'block' whicheve the box happens to be}. Peruse around at
              http://www.javascriptkit.com/cutpastejava.shtml .

              Danny


              On Tue, 24 May 2005 09:50:26 -0700, Darren <news@lan-specialist.com> wrote:
              [color=blue]
              > I have a form that has 10 fields on it. I have made all of them
              > "Required".
              > I also am using vb if statements to decide whether or not each field
              > should
              > be on the page. I am using the vb to compare values in my database and
              > if a
              > certain field equals something then the field is shown on the form. When
              > the form is displayed in the browser, it could any increment of the 10
              > fields on it. My problem is that unless all fields are being shown then
              > the
              > validation does not work and the form submits even with blank fields.
              > Has
              > anyone tackled this kind of a situation before and if so how did they
              > accomplish it? Thanks in advance for your help!
              >
              > Darren
              >
              >[/color]



              --
              Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

              Comment

              Working...