两个日期的验证问题

两个日期的验证问题

本文介绍了两个日期的验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi friends
i have to text box fist is birth date and second is join date and my problem is how to validate difference between 20 year birth date and join date pls help me

and it is possible to validate to validation control

推荐答案


<asp:comparevalidator id="Validator1" runat="server" Display = "None" ControlToCompare="txtDateofBirth" ControlToValidate="txtDateofJoining" ErrorMessage="Date of Birth must be before Date of Joining." Operator="GreaterThan" Type="Date" ></asp:comparevalidator> 




上面的代码验证器中的
将检查加入日期与DOB的值,并显示错误消息

如果用户输入截至2013年的出生日期&加入日期为1989年。



问候。



in the above line of code validator will check the value of joining date with DOB and will show the error message
if user enters the date of birth as of 2013 & date of joining as of 1989.

Regards.


<asp:textbox id="startDate" runat="server" xmlns:asp="#unknown"></asp:textbox>
       <asp:textbox id="endDate" runat="server" xmlns:asp="#unknown"></asp:textbox>
       <asp:customvalidator id="cv" runat="server" validationgroup="vg" clientvalidationfunction="validateYears" errormessage="invalid date" display="None" xmlns:asp="#unknown"></asp:customvalidator>
       <asp:button id="btn" runat="server" text="Click" validationgroup="vg" xmlns:asp="#unknown" />
       <asp:validationsummary id="vs" runat="server" validationgroup="vg" showmessagebox="true" showsummary="false" xmlns:asp="#unknown" />





并添加一个js验证方法





and add a js validation method

function validateYears(source, args) {
            startYear = new Date(document.getElementById("startDate").value);
            endYear = new Date(document.getElementById("endDate").value);
            alert(endYear.getFullYear() - startYear.getFullYear() >= 20);
            args.IsValid = endYear.getFullYear() - startYear.getFullYear() >= 20;
        }





此处的功能获得多年的差异并验证差异为20年,您可能还想考虑几个月或几天的差异。



但是,我不建议使用日期文本框,因为用户可能会使用多种方式输入日期



the function here gets the difference in years and validates for a difference of 20 years, you might also want to consider difference in months or days.

However, i would not recommed using text boxes for dates as users may use multiple ways to enter a date


这篇关于两个日期的验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 17:37