Javascript表单验证日期不起作用

时间:2014-01-17 14:24:04

标签: javascript

我在html表单上运行此javascript。真的很难让这个工作,没有弹出,没有任何警报。请帮忙。我的代码可能很糟糕,但我只用了4个星期的javascript

    function validate(form){

var vRunnerId = document.forms["submitrunnertime"]["RunnerId"].value;
var vEventId = document.forms["submitrunnertime"]["EventId"].value;
var vDate = document.forms["submitrunnertime"]["Date"].value;
var vFinishTime = document.forms["submitrunnertime"]["FinishTime"].value;
var vPosition = document.forms["submitrunnertime"]["Position"].value;
var vCategoryId = document.forms["submitrunnertime"]["CategoryId"].value;
var vAgeGrade = document.forms["submitrunnertime"]["AgeGrade"].value;
var vPB = document.forms["submitrunnertime"]["PB"].value;
var validFormat = /^\d{4}-\d{2}-\d{2}$/;

    if (/\D/.test(vRunnerId)){
      alert("Please only enter numeric characters only for your Runner ID");
      return false;
      }

    if (/\D/.test(vEventId)){
      alert("Please only enter numeric characters only for your Event ID");
      return false;
      }

    if (!validFormat.test(vDate){
      alert("Please enter date in YYYY-MM-DD");
      return false;
      }

      return true;
}

2 个答案:

答案 0 :(得分:0)

我没有看到你的vDate被定义在该函数的任何位置,所以看起来你忘了将它添加到你的javascript中。

答案 1 :(得分:0)

您正在抓取<input>值两次。来到这里:

var vDate = document.forms["submitrunnertime"]["Date"].value;

再次检查:

if (!validFormat.test(vDate.value)){

您正试图找到.value的{​​{1}},我认为这会引发错误。