[Javascript] Control in the select

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • viki1967
    Contributor
    • Oct 2007
    • 263

    [Javascript] Control in the select

    Hi everyone.

    I have this web page:

    [php]

    <html>

    <head>
    <SCRIPT>
    <!--

    function validate(thefor m)

    {

    for (var a = 0; a < theform.element s.length; a++)
    {
    var campo = theform.element s[a];

    if (campo.value.le ngth <= 0)

    {
    window.alert('K O');
    campo.focus();
    return false;
    }
    }

    return(true);

    }

    // -->

    </script>


    </head>

    <body>

    <form action="paginat .asp" method="POST" onSubmit="retur n validate(this)" >

    <select size="1" name="D1">
    <option>Selec t</option>
    <option value="CCC">CCC </option>
    <option value="AAA">AAA </option>
    <option value="BBB">BBB </option>
    </select>

    <select size="1" name="D2">
    <option>Selec t</option>
    <option value="CCC">CCC </option>
    <option value="AAA">AAA </option>
    <option value="BBB">BBB </option>
    </select>

    <input type="submit" value="Invia" name="B1">
    </form>

    </body>

    </html>


    [/php]

    I check that the value of select D2 is always different from value select D1.

    For example:

    1) value of select D1 = AAA
    2) value of select D2 = BBB

    it's right.

    1) value of select D1 = AAA
    2) value of select D2 = AAA

    it's wrong.

    Can you help me for to have this control in the function validate(thefor m) ?

    many thanks
    Viki
  • pronerd
    Recognized Expert Contributor
    • Nov 2006
    • 392

    #2
    Code:
        ....
        ....
        ....
    function validate(theform) { 
    
            var field1 = document.getElementById('d1').value;
            var field2 = document.getElementById('d2').value;
    
            if( field1 == field2 ) {
                 alert('Values can not be the same.');
                 return false;
            }
    
            if( field1.length() < 1 || field1.length() < 1 ) {
                 alert('Fields can not be blank.');
                 return false;
            }
    
        }
    		
        return(true);
    }
    
    // -->
    
     </script>
        ....
        ....
        ....
    <select size="1" name="D1" id="d1" >
        ....
        ....
        ....
    <select size="1" name="D2" id="d2" >
        ....
        ....
        ....

    Comment

    • Logician
      New Member
      • Feb 2007
      • 210

      #3
      Originally posted by pronerd
      Code:
              if( field1.length() < 1 || field1.length() < 1 )
      String.length is not a method in JS.

      Comment

      • viki1967
        Contributor
        • Oct 2007
        • 263

        #4
        many thanks, but this area not work:

        [php]

        if( field1.length() < 1 || field1.length() < 1 ) {
        alert('Fields can not be blank.');
        return false;
        }


        [/php]

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Read Logician's reply. length is not a method (it's a property), so field1.length() should be field1.length.

          Comment

          • viki1967
            Contributor
            • Oct 2007
            • 263

            #6
            Yes Acoder... I read Logician's reply...
            My reply is the confirmation that String.length is not a method in JS.

            thanks,
            Viki

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              You didn't even need to check the length. You could've just compared it to the empty string: field1 == "". Note also that the second field1 should be field2.

              Comment

              Working...