用于日期验证的javascript switch case语句 [英] javascript switch case statement for date validation

查看:108
本文介绍了用于日期验证的javascript switch case语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用切换用例称呼此方法:

How do I call this methos using switch case:

function validateDate() {
          var fromDate = document.getElementById('<%=txtFrom.ClientID %>').value;
          var toDate = document.getElementById('<%=txtTo.ClientID %>').value;

          if (fromDate == "" && toDate == "")
          {
          alert("from & To date Should not be Empty");

      }
      if (fromDate != "" && toDate == "") {
          alert("Please enter to Date");
      }
      if (fromDate >toDate) {
          alert("From date should be less than To Date:");
      }



}



}

推荐答案

您不能.开关没有两个参数,也没有>或!=
you can''t. A switch does not have two arguments, and does not do > or !=




首先,我发现您的最后一个if语句存在一个问题,即您检查 fromDate>的代码. toDate 不正确.它会给您不正确的结果.

正确处理的唯一方法是更改​​日期格式.将您的日期转换为 yyyyMMdd 的形式,然后进行比较(例如数值)就可以了.但是您直接从文本框中匹配日期,并且我认为用户不会以yyyyMMdd格式插入日期.

还有一个变化,

Hi,

First of all i found one issue with your last if statement, your code for checking fromDate > toDate is incorrect. it will give you incorrect result.

The only way to get it right is to change your Date format. Convert your date in form of yyyyMMdd and then comparison like a numeric value will be fine. But you are directly matching your date from textbox and i don''t think user will insert date in yyyyMMdd format.

One more change,

if (fromDate == "" && toDate == "")
          {
          alert("from & To date Should not be Empty");
      }
      if (fromDate != "" && toDate == "") {
          alert("Please enter to Date");
      }


如果您不需要指定哪个日期为空,则可以使用以下代码进行更改.或者,您也可以在文本框中添加RequiredField Validator(仅当您在ASP.NET中工作时).


can be changed with below code if you do not need to specify which date is empty. Or you can also add RequiredField Validator in your textbox(Only if you are working in ASP.NET).

if (fromDate == "" || toDate == "")
{
    alert("from Or To date Should not be Empty");
}



其次,您不能在javascript或C#代码中将Swith大小写用于范围.在 [



Second, you can not use Swith case with range either in javascript or in C# code. Check Answer in this[^] discussion.

Thanks
-Amit Gajjar


这篇关于用于日期验证的javascript switch case语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆