简体   繁体   English

验证日期输入错误

[英]Trouble validating date entry

Needing to validate that user inputs date that is not before 2014 or after the current date. 需要验证用户输入的日期不晚于2014年或当前日期之后。 I tried splitting the date entry by "/" and then using parseInt to catch if the [2] spot was less than 2014 but it doesn't seem to work! 我尝试用“ /”分隔日期条目,然后使用parseInt来捕获[2]点是否小于2014,但似乎不起作用!

 <!DOCTYPE HTML> <html> <head> <style> form {border-style: inset; border-color: blue; height: 360px; width: 775px; margin: auto; } </style> <script> //Declares 3 arrays var fullNames = new Array(100); var dates = new Array(100); var opinions = new Array(100); //Global variable var numOfRatings = 0; </script> <script> //Validates fields are not empty, date is correct then assigns data to arrays function validateData() { var fullNameStr = document.getElementById("FullName").value; var dateStr = document.getElementById("Date").value; var opinionStr = document.getElementById("opinion").value; if (!fullNameStr||!opinionStr||!dateStr) { alert("Please complete all fields."); return; } else { var dateVal = dateStr; var dateParts = dateVal.split("/"); if (parseInt(dateParts[2]) < "2014") { alert("Invalid date!"); } else { fullNames[numOfRatings] = fullNameStr; dates[numOfRatings] = dateStr; opinions[numOfRatings] = opinionStr; numOfRatings++; document.getElementById("FullName").value = ""; document.getElementById("Date").value = ""; document.getElementById("opinion").value = ""; } } } </script> <script> //Displays data held in arrays function displayData() { var col; var pos; document.getElementById("list").value = "FULL NAME DATE RATING\\n"; for (pos = 0; pos < numOfRatings; pos++) { document.getElementById("list").value += fullNames[pos]; for (col = fullNames[pos].length; col <= 25; col++) document.getElementById("list").value += " "; document.getElementById("list").value += dates[pos]; for (col = dates[pos].length; col <= 15; col++) document.getElementById("list").value += " "; document.getElementById("list").value += opinions[pos]+ "\\n"; } } </script> <script> //Clears form function clearData() { var pos; for (pos = 0; pos < numOfRatings; pos++) { fullNames[pos] = ""; dates[pos] = ""; opinions[pos] = ""; } numOfRatings = 0; } </script> </head> <h1 style ="text-align:center; border-bottom:1px solid #999999">Internet Technologies Membership</h1> <form name ="ratingForm"> <table cellpadding="10"> <td> <tr> <td>Full Name:</td> <td><input type="text" id="FullName" name="FullName"></td> <td>Date:</td> <td><input type="date" id="Date" name="Date"></td> <td>Opinion:</td> <td> <select name="opinion" id="opinion"> <option value="&#9733;&#9733;&#9733;&#9733;&#9733;">Excellent</option> <option value="&#9733;&#9733;&#9733;&#9733;">Very Good</option> <option value="&#9733;&#9733;&#9733;">Good</option> <option value="&#9733;&#9733;">Fair</option> <option value="&#9733;">Poor</option> <option value="0">Very Bad</option> </select> </td> </tr> </table> <br> <center> <textarea name="list" id="list" rows="10" cols="100"> </textarea> <br> <br> <input type="button" value="RATE" onClick="validateData();"> <input type="button" value="DISPLAY" onClick="displayData();"> <input type="reset" value="CLEAR" onClick="clearData();"> <br> </center> </form> </html> 

if (parseInt(dateParts[2]) < "2014") {

The above part of code is checking the dateParts[2] (an Int) with a string. 上面的代码部分使用字符串检查dateParts [2](一个Int)。

You have to make the "2014" part as an Integer too. 您还必须将“ 2014”部分设置为整数。

So the above line should be 所以上面的行应该是

if (parseInt(dateParts[2]) < 2014) {

Also I would suggest using MomentJS for any DateTime related functionality. 我也建议对任何与DateTime相关的功能使用MomentJS

The date will be retrieved in YYYY-MM-DD format irrespective of what is displayed in the input box. 不管输入框中显示的内容如何,​​都将以YYYY-MM-DD格式检索日期。 Try to split it by hyphen (-). 尝试用连字符(-)分割。

Also while comparing use 2014 as a number instead of string 同样在比较使用2014作为数字而不是字符串时

var dateParts = dateVal.split("-");
if (parseInt(dateParts[0]) < 2014)

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

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