日期在Chrome上的JavaScript验证

huangapple go评论137阅读模式
英文:

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[&quot;reservationForm&quot;][&quot;name&quot;].value;
  if (n !=&quot;&quot;)
  {cnt++;}

//Mobile Number Validation
var m = document.forms[&quot;reservationForm&quot;][&quot;tel&quot;].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[&quot;reservationForm&quot;][&quot;number&quot;].value;
if(g&gt;0 &amp;&amp; g&lt;=15)
  {cnt++;}

// Date Validation
var GivenDate = document.forms[&quot;reservationForm&quot;][&quot;Date&quot;].value;
var CurrentDate = new Date();
GivenDate = new Date(GivenDate);

if(GivenDate &gt; CurrentDate)
{cnt++;datecnt++}

  //Final Dialog Box Check
  if(cnt==5)
  {alert(&quot;Reservation Confirmed&quot;);}

  if(datecnt==0)
  {alert(&quot;Please enter a date before the current date.&quot;);
    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 &lt; CurrentDate){ ...`

Hope this helps

**[EDIT]**
Ok I didn&#39;t see, that some of my points were pointed out in the comments ;)

</details>



huangapple
  • 本文由 发表于 2020年1月7日 01:19:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定