简体   繁体   English

JAVASCRIPT在表单中验证日期

[英]JAVASCRIPT validate date in the form

I tried to do the javascript to validate the date enter is in correct way. 我试图用javascript来验证日期输入是否正确。 For example, for ETA(estimate time arrival), the date should be earlier than ETD(estimate time departure). 例如,对于ETA(估计到达时间),日期应早于ETD(估计时间离开)。

This is the form: 这是形式:

   <form>
      <label>ETA</label>
             <input  id="etadate" name="etadate" type="date" />
             <input name="etatime" type="time"/> 
      <label>ETD</label>
             <input  id="etddate" name="etddate" type="date"/>
             <input name="etddime" type="time"/>                                                                                        
   </form>

And for the javascript: 而对于javascript:

function validate1
 {
       var x = document.getElementById("etadate");
       var y = document.getElementById("etddate");

       if (x < y)
       {
          true;
       }
       else
       {
          false;
       }
}

When i tried to run the code, it didnt give any notice to the user that the date for eta should be earlier than etd. 当我试图运行代码时,它没有通知用户eta的日期应早于etd。 I dont know where to adjust so the code run correctly. 我不知道在哪里调整,所以代码运行正常。

Use like this - 像这样使用 -

function validate1
{
   var x = new Date(document.getElementById("etadate").value);
   var y = new Date(document.getElementById("etddate").value);

   return x<y;
}

There are a couple of issues: 有几个问题:

var x = document.getElementById("etadate");

will return a reference to the input element, which is an object. 将返回对input元素的引用,该元素是一个对象。 Objects shouldn't be compared using the < operator. 不应使用<运算符比较对象。 You need: 你需要:

var x = document.getElementById("etadate").value;

so that x is now a string. 所以x现在是一个字符串。

Where input type date is supported, the browser should successfully parse the string to a date, so you might do: 在支持输入类型日期的情况下,浏览器应该成功地将字符串解析为日期,因此您可以执行以下操作:

var x = new Date(document.getElementById("etadate").value);

However, in some browsers the returned string is an ISO 8601 formatted string like 2016-05-23, which will be parsed as UTC so for those west of GMT, it will appear to be one day earlier. 但是,在某些浏览器中,返回的字符串是ISO 8601格式的字符串,如2016-05-23,它将被解析为UTC,因此对于GMT以西的人来说,它似乎是提前一天。 You will need to deal with that. 你需要处理这个问题。

Then there are browsers that don't support input type date, there are questions on SO about that. 然后有些浏览器不支持输入类型日期,有关于此的问题。

Anyway, you need to: 无论如何,你需要:

  1. Get the value of each input 获取每个输入的值
  2. Check that they generate a valid dates 检查它们是否生成有效日期
  3. If they do, compare the dates, so something like: 如果他们这样做,请比较日期,如下所示:

 function validate1 (form) { var x = new Date(form.etadate.value); var y = new Date(form.etddate.value); if (isNaN(+x) || isNaN(+y)) { alert("One of the dates is invalid"); return false; } if (x < y) { alert('ETA is before ETD'); } else { alert('ETD is before ETA'); } } 
 <form onsubmit="validate1(this); return false;"> <label>ETA</label> <input id="etadate" name="etadate" type="date"> <input name="etatime" type="time"/> <label>ETD</label> <input id="etddate" name="etddate" type="date"> <input name="etdtime" type="time"> <button>submit</button> </form> 

I've blocked submission with return false , naturally the function and listener should return true or false depending on the outcome of the comparison. 我已经使用return false阻止了提交,自然函数和监听器应该返回true或false,具体取决于比较的结果。

Also, the function declaration in the OP doesn't have a parameter list, so the syntax is invalid (fixed in the above). 此外,OP中的函数声明没有参数列表,因此语法无效(在上面已修复)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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