javascript没有在onBlur上运行

时间:2017-04-02 04:18:18

标签: javascript

验证日期字段的功能不在下面的onBlur上执行。我不确定我在这里失踪了什么。只是试图测试一个简单的验证。

   <div class="cell">
   <label>DOB</label><br>       
   <input id="dob" type="text" placeholder="01/01/1980" onBlur="checkForm(this.value);">
   </div>


   <script type="text/javascript">


 function checkForm(form)
 {
// regular expression to match required date format
re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;

if(form.dob.value != '' && !form.dob.value.match(re)) {
  alert("Invalid date format: " + form.dob.value);
  form.dob.focus();
  return false;
}



alert("All input fields have been validated!");
return true;
 }

1 个答案:

答案 0 :(得分:1)

 <div class="cell">
   <label>DOB</label><br>       
   <input id="startdate" type="text" placeholder="01/01/1980" onblur="checkForm()">
   <script>
    function checkForm()
 {
// regular expression to match required date format
re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
var date = document.getElementById("startdate").value;
alert(date);
if(date != '' && !date.match(re)) {
  alert("Invalid date format: " + date);
  date.focus();
  return false;
}
alert("All input fields have been validated!");
return true;
 }
   </script>
   </div>