验证输入的日期早于或等于javascript中的当前日期 [英] To validate entered date is earlier than or equal to current date in javascript

查看:67
本文介绍了验证输入的日期早于或等于javascript中的当前日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我一直在试用一个代码,其中有一个控制输入日期,输入的日期必须经过验证,输入的日期必须早于或等于当前日期。

有谁能帮助我实现这个目标?

Hi everyone,
I have been trying out for a code where there is a control to enter a date and the entered date has to be validated such that entered date must be earlier than or equal to current date.
Can anyone please help me achieve this?

推荐答案

你的问题不明确......

我建议你看看: http://www.w3schools.com/js/js_obj_date.asp [ ^ ]
Your question is not clear...
I suggest you to see this: http://www.w3schools.com/js/js_obj_date.asp[^]






希望您以dd / MM / yyyy格式输入日期。所以这里是解决问题的示例代码。



在你的.aspx页面中放置一个TextBox。



Hi,

Hope you are entering your date in dd/MM/yyyy format. So here is the sample code for the solution to your problem.

Place a TextBox in your .aspx page like this.

<asp:TextBox ID="txtDate" runat="server" Width="100px" onblur="checkDate(this.value);"></asp:TextBox>





Place .aspx页面中的以下脚本





Place the below script in your .aspx page

<script type="text/javascript" language="javascript">

    function checkDate(input) {
    
    var today = new Date();
    var todayFormat = today.format("MM/dd/yyyy");
    var endDate = formatDate(input);
    if (endDate > todayFormat) {
        alert("Entered date must be earlier than or equal to current date!");
    }
}

function formatDate(input) {
            var datePart = input.match(/\d+/g);
            var day = datePart[0] 
            var month = datePart[1]
            var year = datePart[2];
            var myDate = month + '/' + day + '/' + year;
            return myDate;
        }
</script>





希望这可以解决您的问题。如果您遇到任何问题,请告诉我。



问候,

Manoj



Hope this will solve your problem. Please let me know if you face any issues.

Regards,
Manoj


继承人我的代码,它的工作原理:)

这绝对是一种简单的代码,但它的工作原理:)

感谢Manoj,Maciej Los为我提供了如何工作的基本思路为此。















< title>新文件











function checkDate(){

var input = document.form1.dateString.value;

var arr = input.split(/);

var newdate = new Date();

var sysday = newdate.getDate();

var sysmonth = newdate.getMonth();

var sysyear = newdate.getFullYear();

if((arr [0]& gt; sysday)||(arr [1]& gt; sysmonth)||(arr [ 2]& gt; sysyear)){

提醒(错误的日期);

}

}













日期








Heres my code and it works :)
It is definitely trivial kind of code but it works :)
Thanks Manoj,Maciej Los for providing me the basic idea of how i must work for this.







<title> New Document





function checkDate(){
var input = document.form1.dateString.value;
var arr = input.split("/");
var newdate= new Date();
var sysday = newdate.getDate();
var sysmonth=newdate.getMonth();
var sysyear=newdate.getFullYear();
if((arr[0] &gt; sysday) || (arr[1] &gt; sysmonth) || (arr[2] &gt; sysyear)){
alert("wrong date");
}
}






Date





这篇关于验证输入的日期早于或等于javascript中的当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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