英文:
Date Validation in Javascript on Chrome
问题
我已经编写了一个与JavaScript链接的HTML表单,用于检查输入的日期是否在当前日期之后。该程序在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);
}
}
英文:
I have coded an HTML form which is linked to JavaScript to check whether the entered date in after the current date. The program works properly on Microsoft Edge and Edge Insider but isn't returning any value in 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);
}
}
</details>
# 答案1
**得分**: 2
以下是要翻译的内容:
实际上,有多个不太正确的地方...
第一个,可能也是最重要的,是JavaScript中的关键字 `return`,不允许出现在函数外部。
第二个:您有太多的括号。最后的 `}` 是不必要的。
第三个:如果您希望给定日期在当前日期之后,您必须修改您的警告,因为它表示相反的情况。如果您希望给定日期在当前日期之前,您应该如下检查:`if (GivenDate < CurrentDate){ ...`
希望这有所帮助。
**[编辑]**
好的,我没有看到,我的一些观点已经在评论中指出了 ;)
<details>
<summary>英文:</summary>
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.
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. If you want the givenDate to be BEFORE the current date, you should check as follows: `if (GivenDate < CurrentDate){ ...`
Hope this helps
**[EDIT]**
Ok I didn't see, that some of my points were pointed out in the comments ;)
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论