Date verification problem

xiaoxiao2021-03-06  19

Date verification problem ... (Regular expression plus javascript) [Date: 2004-04-05] [From: DOTNET China Net]

[Favorite this page]

code:

1. Use the date picker, many friends have done a date selector, and I have two, I think this approach is the best because it is eliminated the date of judgment.

2. Use the .NET comes with the verification control, huh, it is to use the regular expression, I think it is imperfect, such as the 31,30 No. 31, 30 in February.

3. JavaScript script makes judgment, the following is the date verification that I spent an hour, is a relatively perfect verification, you can judge the leap year, there are two main functions, where checkdate is the date of judgment, such as 2002-02-09, Checkdate is a date of judgment, such as 2002-02-09 12:09:09, when verifying, such as determining the button to Button1, should add Button1.attributes.Add ("Onclick", "Return (' TXT_DATE '); ");

JS file code:

_________________________________________________________________

Function FOB (N, D)

{

VAR P, I, X; IF (! d) d = document;

IF ((p = n.indexof ("?"))> 0 && parent.frames.length)

{

D = parent.frames [n.substring (p 1)]. document;

n = n.substring (0, p);

}

IF (! (x = d [n]) && D.all)

X = D.All [N];

For (i = 0;! x && i

X = D.Forms [i] [n];

For (i = 0;! x && D.layers && i

X = fob (n, d.layers [i] .document);

Return X;

}

Function Checkdate (InPar)

{

Var flag = true;

getdate = FOB (INPAR) .value;

IF (GetDate.Search (/ ^ [0-9] {4} - (0 [1-9] | [1-9] | ((0 [1-9] | [1 -9]) | 1 [0-9] | 2 [0-9] | 3 [0-1]) $ /) == - 1) // Judgment the input format is correct

{

Flag = false;

}

Else

{

Var year = getdate.substr (0, getdate.indexof ('-')) // get the year

// The following operation is available

VAR transition_month = getdate.substr (0, getdate.lastIndexof ('-'));

Var month = transition_month.substr (Transition_MontH.lastIndexof ('-') 1, Transition_Month.Length;

IF (Month.Indexof ('0') == 0)

{

Month = month.substr (1, Month.length);

}

// Date below

Var day = getdate.substr ('-') 1, getDate.length);

IF (DAY.INDEXOF ('0') == 0) {

Day = day.substr (1, day.length);

}

Flag = true;

}

IF ((MONTH == 4 || Month == 6 || Month == 9 || Month == 11) && (day> 30)) // 4, 6, 9, November dates can not exceed 30

{

Flag = false;

}

IF (Month == 2) // Judgment February

{

Leepyear (Year))

{

IF (DAY> 29 || Day <1) {flag = false;

}

Else

{

IF (DAY> 28 || Day <1) {flag = false;}

}

}

Else

{

Flag = true;

}

IF (Flag == False)

{

Alert ("The date you entered is not legal!");

Return False;

}

}

Function CheckTime (InPar)

{

Var flag = true;

getdate = FOB (INPAR) .value;

IF (GetDate.Search (/ ^ [0-9] {4} - (0 [1-9] | [1-9] | ((0 [1-9] | [1 -9]) | 1 [0-9] | 2 [0-9] | ((0 [1-9] | [1-9]) | 1 [0-9] | 2 [0-4]): ((0 [1-9] | [1-9]) | [1-5] [0-9]): (0 [1-9] | [1-9]) | [1-5]) $ /) == - 1) // Judgment the input format is correct

{

Flag = false;

}

Else

{

Var year = getdate.substr (0, getdate.indexof ('-')) // get the year

// The following operation is available

Var transition_month = getdate.substr (0, getdate.lastIndexof (''));

Transition_month = getdate.substr (0, getdate.lastIndexof ('-'));

Var month = transition_month.substr (Transition_MontH.lastIndexof ('-') 1, Transition_Month.Length;

IF (Month.Indexof ('0') == 0)

{

Month = month.substr (1, Month.length);

}

// Date below

Var transition_day = getdate.substr (0, getdate.lastIndexof (''));

Var day = transition_day.substr (Transition_Day.lastIndexof ('-') 1, Transition_Day.Length;

IF (DAY.INDEXOF ('0') == 0)

{

Day = day.substr (1, day.length);

}

Flag = true;

}

IF ((MONTH == 4 || Month == 6 || Month == 9 || Month == 11) && (day> 30)) // 4, 6, 9, November dates can not exceed 30

{

Flag = false;

}

IF (Month == 2) // Judgment February {

Leepyear (Year))

{

IF (DAY> 29 || Day <1) {flag = false;

}

Else

{

IF (DAY> 28 || Day <1) {flag = false;}

}

}

Else

{

Flag = true;

}

IF (Flag == False)

{

Alert ("The date you entered is not legal!");

Return False;

}

}

// Judgment whether it is the year

// Parameter INTYEAR represents the value of the year

// Return True: Yeah False: Not a leap year

Function Leepyear (Interedation) {

IF (intYear% 100 == 0) {

IF (intYear% 400 == 0) {Return True;}

}

Else {

IF ((INTYEAR% 4) == 0) {Return True;}

}

Return False;

}

转载请注明原文地址:https://www.9cbs.com/read-52244.html

New Post(0)