从API验证国家/地区调用

时间:2015-10-26 07:32:21

标签: javascript validation api country

<div class='input1'>
                    <select name="gender" class="gender" id="gender">
                        <option value="">Gender</option>
                        <option value="male">Male</option>
                        <option value="female">Female</option>
                    </select>
</div>

JS

reg_gender = /(male|female)/;
      if(form.gender.value == "" && (!reg_gender.test(form.gender.value)) ) {
          alert("Please select gender!");
          form.gender.focus();
          return false;
      }
  

EDITTED

我应该如何使用类似于性别的方法验证国家/地区?但是,我的国家没有硬编码。它是从api打来的。 enter image description here

1 个答案:

答案 0 :(得分:0)

HTML 进行简单的更改:

<div class='input1'>
   <select id="gender" name="gender" class="gender">
     <option value="">Gender</option> <!-- make change value="" -->
     <option value="male">Male</option>
      <option value="female">Female</option>
    </select>
</div>

还在 javascript 中添加一些代码:

reg_gender = /(male|female)/;
if(form.gender.value == "" && (!reg_gender.test(form.gender.value)) ) {
    alert("Please select gender!");
    form.gender.focus();
    return false;
}

此表达式/(male|female)/检查值是male还是female