验证js中的开始日期和结束日期 [英] validation for start and end date in js

查看:93
本文介绍了验证js中的开始日期和结束日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
任何人都可以显示如何编写Java脚本代码以用于开始日期和结束日期验证....

hi everyone,
Can anyone show how to write the java script code for start date and end date validation ....

推荐答案

验证什么?您要接受什么格式?可接受值的含义是什么-除了显而易见的是,结束日期必须大于开始日期?

但您可以从以下内容开始:

Validating what? What is the format you want to accept? What is the semantic of acceptable values - besides the obvious, that end date has to be greater than start date?

But you can start out from this one:

<script>
function IsValidDate(date){
    var D,M,Y;
    var re= /^(\d{4})-(\d{1,2})-(\d{1,2})


/; // YYYY-MD,YYYY-MM-D,YYYY-M-DD,YYYY-MM-DD var match = date.match(re); 如果(!match)返回 false ; Y = 1 * match [ 1 ]; M = 1 * match [ 2 ]; D = 1 * match [ 3 ]; var dt = 日期(是, M-1,D); 如果(dt.getDate()!= D){ 返回(错误); } 其他 如果(dt.getMonth()!= M-1){ 返回(错误); } 其他 如果(dt.getFullYear()!= Y){ 返回(错误); } 返回(dt); } 函数 ValidateDateInput(id){ var result = IsValidDate( document .getElementById(id).value); 如果(结果) 文档 .getElementById(id).style.color = " ; 其他 文档 .getElementById(id).style.color = " ; 返回结果; } 函数 validate(){ var date1 = ValidateDateInput(" ); var date2 = ValidateDateInput(" ); 如果(date1&& date2&& date1< date2) 文档 .getElementById(" ) .style.backgroundColor = " ; 其他 文档 .getElementById(" ) .style.backgroundColor = " ; } </script> < div id = " > 日期1:<输入类型= " id = " name = " value = " > Date2:<输入类型= " id = " name = " value = " > < button onclick = " >让参见</button> </div>
/; //YYYY-M-D, YYYY-MM-D, YYYY-M-DD, YYYY-MM-DD var match = date.match(re); if(!match) return false; Y = 1*match[1]; M = 1*match[2]; D = 1*match[3]; var dt = new Date(Y,M-1,D); if(dt.getDate()!=D){ return(false); } else if(dt.getMonth()!=M-1){ return(false); } else if(dt.getFullYear()!=Y){ return(false); } return(dt); } function ValidateDateInput(id) { var result = IsValidDate(document.getElementById(id).value); if (result) document.getElementById(id).style.color="green"; else document.getElementById(id).style.color="red"; return result; } function validate(){ var date1 = ValidateDateInput("date_1"); var date2 = ValidateDateInput("date_2"); if (date1 && date2 && date1 < date2 ) document.getElementById("dates").style.backgroundColor="green"; else document.getElementById("dates").style.backgroundColor="red"; } </script> <div id="dates"> Date1: <input type="text" id="date_1" name="date_1" value="2012-02-10"> Date2: <input type="text" id="date_2" name="date_2" value="2012-02-12"> <button onclick="validate();">Let's see</button> </div>


我认为这对您有帮助



Hi i think it''s helpfull to you



function checkDOJ(sender, args) {
               if (sender._selectedDate < new Date()) {
                   alert("You cannot select a day earlier than today!");
                   sender._selectedDate = new Date();
                   // set the date back to the current date
                   sender._textbox.set_Value(sender._selectedDate.format(sender._format))
               }
           }

           function checkDOB(sender, args) {
               if (sender._selectedDate > new Date()) {
                   alert("You cannot select a day Greater than today!");
                   sender._selectedDate = new Date();
                   // set the date back to the current date
                   sender._textbox.set_Value(sender._selectedDate.format(sender._format))
               }



关于...........

巴拉吉
www.leaderbalaji.blogspot.com



Regards...........

Balaji
www.leaderbalaji.blogspot.com


这篇关于验证js中的开始日期和结束日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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