[英]Regular Expression for Multiple Dates
我有三個文本框
Date Of Birth
第一
入會Date of Joining
第二名
Date of Leaving
第三名
我希望以適當的方式驗證正則表達式。 例如:出生日期應小於加入日期和離開日期。
此外,離開日期應始終大於加入日期。 讓我知道如何正確驗證
僅使用正則表達式基本上不可能實現日期驗證,2月29日是更難解決的問題之一。 在正則表達式中,日期比較更加困難。
尋找另一種方法 。
您可以使用JavaScript使用客戶端驗證
tbStartDateID和tbEndDateID是文本框控件的ID,分別在其中輸入開始日期和結束日期。
JavaScript函數:
function CompareDates(tbStartDateID, tbEndDateID) {
var startDate = document.getElementById(tbStartDateID).value;
var endDate = document.getElementById(tbEndDateID).value;
//Difference in milliseconds
var timeDiff = Date.parse(endDate) - Date.parse(startDate);
if (Date.parse(endDate) > Date.parse(startDate)) {
alert("It is okay, end date is greater than start date");
}
else {
alert("Start Date must be lesser than end Date");
}
}
You cannot use Regular Expressions for validating dates.
Instead you can use something like this :
**HTML :**
<input type="date" id="dob" name="dob" runat="server" />
<input type="date" id="joinDate" name="jDate" runat="server" />
**C# Code :**
protected Boolean Validate_DateOfBirth()
{
int dateDOB, joiningDt;
string joiningDate = joinDate.Value; ;
string varDOB = dob.Value;
varDOB = varDOB.Replace(@"-", "");//Our string will be in yyyyMMdd format now.
joiningDate = joiningDate.Replace(@"-", "");
dateDOB=int.Parse(varDOB);
joiningDt = int.Parse(joiningDate);
if (dateDOB > joiningDt){
//Raise your error here.
return false;
}
else{
//Everything id fine..
return true;
}
}
You can implement the similar thing for comparing date of joining and
date of leaving..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.