Validate select

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

    Validate select

    Hello everybody! :-D

    I am trying to validate a select object in a form. It does not work fine
    in Explorer 6.0 because it always send the form, but in Netscape 7.0 it
    works really fine. The code is below:

    //----------------------------------------------------------------------
    <head>
    <script >
    function validate()
    {
    if( document.result s.p1.value=="1"
    alert('Value not valid')
    else
    document.result s.submit()

    }
    </script>
    </head>

    <FORM name=result action="page.ph p" method=post>
    <TABLE cellSpacing=0 cellPadding=0 align=center>
    .....
    .....
    <TR>
    <TD align=center>
    <select name=p1 id=p1>
    <option selected>1</option>
    <option>X</option>
    <option>2</option>
    </select>
    </TD>
    </TR>
    .....
    .....
    .....
    .....
    <TR class="style">
    <TD align=center>
    <INPUT type=button value="Send" name=Submit onclick="valida te()">
    </TD>
    </TR>
    </table>
    </form>
    //----------------------------------------------------------------------

    I think it's very very very strange. Isn't it? :?

    Any ideas, plese?

    Thanks!!!


  • Ivo

    #2
    Re: Validate select

    "Alonso" wrote[color=blue]
    > I am trying to validate a select object in a form. It does not work fine
    > in Explorer 6.0 because it always send the form, but in Netscape 7.0 it
    > works really fine. The code is below:
    >
    > function validate()
    > {
    > if( document.result s.p1.value=="1"[/color]

    Do you see the opening bracket? Good. Now do you see the corresponding
    closing bracket? Right, unbalanced brackets, it 's a miracle that Netscape
    gave you the impression that all is fine.
    This direct assessing of select values is only possible in newer browsers.
    To cater for versions 4, you need to access the element via the
    selectedIndex property (watch for wrap):
    if(
    document.forms. results.element s.p1[
    document.forms. results.element s.p1.selectedIn dex].value )
    or
    var p = document.forms. results.element s.p1;
    if( p[p.selectedIndex].value=="1" ) ...
    [color=blue]
    > alert('Value not valid')
    > else
    > document.result s.submit()[/color]

    The submit() method should not be called like this, submission is already
    happening by the time that function is called. Form validation functions are
    best set up like this: the function is called "onsubmit" of the form itself,
    not "onclick" of some button. This way all manners of form submission,
    including pressing the Enter key, are captured. The validation returns true
    or false depending on what you have coded, and the forms submits depending
    on that return value. Like so:

    <form ... onsubmit="retur n validate()">

    function validate() {
    if(document.for ms.results.p1.v alue=="1") {
    alert('Value not valid'); return false;
    }
    return true;
    }

    <snip>
    [color=blue]
    > <select name=p1 id=p1>
    > <option selected>1</option>
    > <option>X</option>
    > <option>2</option>
    > </select>[/color]

    These options don't have values, any of them! Compare with:
    <option selected value="1">1</option>
    <option value="X">X</option>

    hth
    --
    Ivo






    Comment

    • Randy Webb

      #3
      Re: Validate select

      Alonso wrote:
      [color=blue]
      > Hello everybody! :-D
      >
      > I am trying to validate a select object in a form. It does not work fine
      > in Explorer 6.0 because it always send the form, but in Netscape 7.0 it
      > works really fine. The code is below:[/color]

      It does not "work really fine" in NS7, view the Javascript Console.

      [color=blue]
      > //----------------------------------------------------------------------
      > <head>
      > <script >
      > function validate()
      > {
      > if( document.result s.p1.value=="1"[/color]

      You are missing the closing ) on the if test:

      if (document.resul ts.p1.value == "1")
      [color=blue]
      > alert('Value not valid')
      > else
      > document.result s.submit()
      >
      > }
      > </script>
      > </head>
      >
      > <FORM name=result action="page.ph p" method=post>
      > <TABLE cellSpacing=0 cellPadding=0 align=center>
      > .....
      > .....
      > <TR>
      > <TD align=center>
      > <select name=p1 id=p1>
      > <option selected>1</option>
      > <option>X</option>
      > <option>2</option>
      > </select>
      > </TD>
      > </TR>
      > .....
      > .....
      > .....
      > .....
      > <TR class="style">
      > <TD align=center>
      > <INPUT type=button value="Send" name=Submit onclick="valida te()">
      > </TD>
      > </TR>
      > </table>
      > </form>
      > //----------------------------------------------------------------------
      >
      > I think it's very very very strange. Isn't it? :?[/color]

      No, see above. But instead of using a button to submit a form, use a
      submit button and onsubmit, and have the function return true or false.


      --
      Randy
      comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

      Comment

      • Lee

        #4
        Re: Validate select

        Alonso said:[color=blue]
        >
        >Hello everybody! :-D
        >
        >I am trying to validate a select object in a form. It does not work fine
        >in Explorer 6.0 because it always send the form, but in Netscape 7.0 it
        >works really fine. The code is below:
        >
        >//----------------------------------------------------------------------
        ><head>
        ><script >
        >function validate()
        >{
        > if( document.result s.p1.value=="1"
        > alert('Value not valid')
        > else
        > document.result s.submit()
        >
        >}
        ></script>
        ></head>
        >
        ><FORM name=result action="page.ph p" method=post>
        ><TABLE cellSpacing=0 cellPadding=0 align=center>
        >....
        >....
        > <TR>
        > <TD align=center>
        > <select name=p1 id=p1>
        > <option selected>1</option>
        > <option>X</option>
        > <option>2</option>
        > </select>
        > </TD>
        > </TR>
        >....
        >....
        >....
        >....
        > <TR class="style">
        > <TD align=center>
        > <INPUT type=button value="Send" name=Submit onclick="valida te()">
        > </TD>
        > </TR>
        ></table>
        ></form>
        >//----------------------------------------------------------------------
        >
        >I think it's very very very strange. Isn't it? :?[/color]

        No, it's not strange. When you click on a Submit button, the
        form is submitted. What you do in the onClick handler should
        not change that.

        Form validation should be performed in the form's onSubmit
        handler. If that handler returns false, the form will not
        be submitted.

        Comment

        • Marky

          #5
          Re: Validate select

          Hi!!!

          Thanks to Ivo, Randy Webb & Lee!! :D I have properly changed my code and
          now it works in both web navigator.

          Thanks from sunny spain!!!

          Comment

          Working...