验证两个选择框

时间:2014-01-19 05:21:41

标签: javascript html

大家好,我需要两个选择下拉框的javascript编码,两者都是语言下拉 我需要条件的验证函数是

  1. 这两个框不应该是默认的select选项,
  2. 这两个框不应该包含相同的值
  3. 如果没有添加第一语言,用户无法添加第二语言(我已经有此代码,请参阅下面的代码)
  4. html代码:

    <select name="language1">
      <option value='Select' selected="selected">Select </option>
      <option value='Bengali'>Bengali </option>
      <option value='Gujarati'>Gujarati </option>
      <option value='Hindi'>Hindi </option>
    </select>
    
    <select name="language2">
      <option value='Select' selected="selected">Select </option>
      <option value='Bengali'>Bengali </option>
      <option value='Gujarati'>Gujarati </option>
      <option value='Hindi'>Hindi </option>
    </select>
    

    这个javascript是我添加的:

    /* if regional language 2 filled without filling language1 then raise an error... */
    if (document.f_edit_shop.language1.value) { }
    else { 
      if (document.f_edit_shop.language2.value) { 
        document.getElementById("validationMessage").innerHTML=
          "&nbsp;<font color='#FF0000'>Error: </font> You can not select Language 2 
          without selecting Language 1 !";
        popup('validationPopup');
        language2.focus();
        return false;
      } 
    }
    

    我也需要这个脚本。 。

1 个答案:

答案 0 :(得分:1)

var lng1 = document.f_edit_shop.language1;
var lng2 = document.f_edit_shop.language2;
var errBox = document.getElementById("validationMessage");

function validate() {
  errBox.innerHTML = '';

  if (lng1.selectedIndex !== 0 || lng2.selectedIndex !== 0)
    errBox.innerHTML += 'Select an option for both boxes<br>';

  if (lng1.selectedIndex === 0 && lng2.selectedIndex !== 0)
    errBox.innerHTML += 'Select a language for first box<br>';

  if (lng1.selectedIndex === lng2.selectedIndex)
    errBox.innerHTML += 'You can\'t select same language twice<br>';
}