简体   繁体   中英

javascript validation of text field based on selection from drop down menu

Hi I have simple fix that is needed. This validation is not working though all other validation works on the page.

   if ((document.getElementById("Authorizing_Company").value == "") && (document.getElementById("Authorizing_Title").selectedIndex == "Other Individual having knowledge of the affairs of the Corporation"))
     {
    alert(Authorizing_Company_msg);
    Authorizing_Company.select();
    Authorizing_Company.focus();
    return(false);              
    }

My HTML

<div class="col-md-6 mb-3">
        <label for="Authorizing_Title">Authorizing Person Position:</label>
      <select name="Authorizing_Title" id="Authorizing_Title" class="form-control form-elements firmnametoggle">
                    <option selected value="">--Select--</option>                         
                    <option value="Current Director">Current Director</option>                
                    <option value="Current Officer">Current Officer</option>                
                    <option value="Other Individual having knowledge of the affairs of the Corporation">Other Individual having knowledge of the affairs of the Corporation</option>                      
                 </select>
  </div>

<div class="col-md-6 mb-3" id="firmname">
        <label for="Authorizing_Company">*Name of Firm or Company:</label>
          <input name="Authorizing_Company" type="text" value="" class="form-control form-elements" id="Authorizing_Company" maxlength="255">
    </div>

This is a logic issue.The problem is on this line of code

(document.getElementById("Authorizing_Title").selectedIndex == "Other Individual having knowledge of the affairs of the Corporation")

In this line of code you try to compare the index of the option which is an integer to a line of string. Therefore is will always be false .

To fix this you can either get the value of the option or just compare with the index.

(document.getElementById("Authorizing_Title").selectedIndex == 3)

OR

(document.getElementById("Authorizing_Title").value== "Other Individual having knowledge of the affairs of the Corporation")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM