[英]Date Validation in Javascript on Chrome
I have coded an HTML form which is linked to JavaScript to check whether the entered date in after the current date.我编写了一个链接到 JavaScript 的 HTML 表单,以检查输入的日期是否在当前日期之后。 The program works properly on Microsoft Edge and Edge Insider but isn't returning any value in Google Chrome该程序在 Microsoft Edge 和 Edge Insider 上正常运行,但在 Google Chrome 中没有返回任何值
function validation()
{
var cnt=0;
var datecnt=0;
//Name Validation
var n = document.forms["reservationForm"]["name"].value;
if (n !="")
{cnt++;}
//Mobile Number Validation
var m = document.forms["reservationForm"]["tel"].value;
var IndNum = /^[0]?[789]\d{9}$/;
if(IndNum.test(m))
{
cnt++;
}
//Email Address Validation
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(reservationForm.email.value))
{cnt++;}
//Number of Guests Validation
var g = document.forms["reservationForm"]["number"].value;
if(g>0 && g<=15)
{cnt++;}
// Date Validation
var GivenDate = document.forms["reservationForm"]["Date"].value;
var CurrentDate = new Date();
GivenDate = new Date(GivenDate);
if(GivenDate > CurrentDate)
{cnt++;datecnt++}
//Final Dialog Box Check
if(cnt==5)
{alert("Reservation Confirmed");}
if(datecnt==0)
{alert("Please enter a date before the current date.");
return (false);
}
}
You actually have multiple things that are not quite correct...你实际上有很多不太正确的事情......
The first one and probably most important is the keyword return
in JavaScript, which is not permitted outside a function.第一个也可能是最重要的一个是 JavaScript 中的关键字return
,这在函数之外是不允许的。
The second one: you have too many parenthesis.第二个:你的括号太多了。 The last }
is not necessary.最后一个}
不是必需的。
The third one: If you want the givenDate to be AFTER the current date, you have to modify your alert as it says the contrary.第三个:如果您希望 givenDate 在当前日期之后,您必须修改您的警报,因为它说的是相反的。 If you want the givenDate to be BEFORE the current date, you should check as follows: if (GivenDate < CurrentDate){ ...
如果您希望 givenDate 在当前日期之前,您应该检查如下: if (GivenDate < CurrentDate){ ...
Hope this helps希望这可以帮助
[EDIT] Ok I didn't see, that some of my points were pointed out in the comments ;) [编辑]好吧,我没有看到,评论中指出了我的一些观点;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.