如何验证输入日期不是更大的日期(未来日期)? [英] How to validate entered date is not greater date(future date)?

查看:88
本文介绍了如何验证输入日期不是更大的日期(未来日期)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有日期选择器并从calendar.how中选择一个日期来验证用户是否选择将来的日期来抛出错误消息。我知道使用jquery隐藏未来的日期。但问题是一个日期选择器使用更多地方所以不要隐藏未来的日期。如何在c#中完成。如果输入未来日期以验证错误消息,则验证日期。

*我只选择日期。日期格式是(年/月/日)



我尝试过:



I have date picker and pick a date from the calendar.how to validate if user select future date to throw the error message.i know the use jquery to hide the future dates.but the problem is one date picker is use more places so don't hide the future dates.how to do it in c#. to validate the date if the future date is entered to throw the error message.
* i pick the date only. date format is (dd/mm/yyyy)

What I have tried:

if (tbxFromDate.Value != "" && Convert.ToDateTime(tbxFromDate.Value) > DateTime.Today)
        {
            Messagebox.Show("From Date should be earlier or equal To Today Date", MessageHelper.MessageType.Warning);
        }



以上代码我将用于验证,但会出现错误消息。

'字符串未被识别为有效的DateTime。'

因为DateTime.Today函数有(mm / dd / yyyy 12:00:00 AM)

如何验证这一个。我使用另一种方法也将文本框值获取到一个字符串,今天日期(仅限日期)存储在另一个字符串中但发生错误。

错误是:不使用>在字符串中(一点点)我忘记了错误消息)。


above code i will used to validation but the error message is occurs.
'String was not recognized as a valid DateTime.'
because DateTime.Today function have (mm/dd/yyyy 12:00:00 AM)
how to validate this one. i used another method also get textbox value to one string and today date(date only) stored in another string but the error occured.
error is: "to not use > in string"(little bit i forget the error message).

推荐答案

实际上,默认文化不会将dd / mm / yyyy识别为日期格式。因此,首先将其转换为有效日期时间,然后比较

Actually the default culture do not recognize dd/mm/yyyy as date format. So first convert it to valid datetime as i shown then compare
using System.Globalization;
        DateTime dt = DateTime.ParseExact(tbxFromDate.Value, "dd/MM/yyyy", CultureInfo.InvariantCulture);
        if (tbxFromDate.Value != "" && dt > DateTime.Today)
        {
            Messagebox.Show("From Date should be earlier or equal To Today Date", MessageHelper.MessageType.Warning);
        }


这篇关于如何验证输入日期不是更大的日期(未来日期)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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