Javascript verification function

Source: Internet
Author: User

/**
* DHTML date validation script.
*/
// Declaring valid date character, minimum year and maximum year
VaR dtch = "-";
VaR minyear = 1900;
VaR maxyears = 2100;
Function isinteger (s ){
VaR I;
For (I = 0; I <S. length; I ++ ){
// Check that current character is number.
VaR c = S. charat (I );
If (C <"0") | (C> "9") return false;
}
// All characters are numbers.
Return true;
}
Function stripcharsinbag (S, bag ){
VaR I;
VaR returnstring = "";
// Search through string's characters one by one.
// If character is not in bag, append to returnstring.
For (I = 0; I <S. length; I ++ ){
VaR c = S. charat (I );
If (bag. indexof (c) =-1) returnstring + = C;
}
Return returnstring;
}
Function daysinfebruary (year ){
// February has 29 days in any year evenly divisible by four,
// Snapshot t for Centurial years which are not also divisible by 400.
Return (Year % 4 = 0 )&&((! (Year % 100 = 0) | (Year % 400 = 0 )))? 29: 28 );
}
Function daysarray (n ){
For (VAR I = 1; I <= N; I ++ ){
This [I] = 31
If (I = 4 | I = 6 | I = 9 | I = 11) {This [I] = 30}
If (I = 2) {This [I] = 29}
}
Return this
}
Function isdate (dtstr ){
VaR daysinmonth = daysarray (12)
VaR pos1 = dtstr. indexof (dtch)
VaR pos2 = dtstr. indexof (dtch, pos1 + 1)
VaR stryear = dtstr. substring (0, pos1)
VaR strmonth = dtstr. substring (pos1 + 1, pos2)
VaR strday = dtstr. substring (pos2 + 1)
Stryr = stryear
 
VaR temstrday = "";
VaR temstrmonth = "";
// Alert (strmonth. Length );
If (strday. charat (0) = "0" & strday. length> 1) temstrday = strday. substring (1)
If (strmonth. charat (0) = "0" & strmonth. length> 1) temstrmonth = strmonth. substring (1)
 
For (VAR I = 1; I <= 3; I ++ ){
If (stryr. charat (0) = "0" & stryr. length> 1) stryr = stryr. substring (1)
}
Month = parseint (temstrmonth)
Day = parseint (temstrday)
Year = parseint (stryr)
If (pos1 =-1 | pos2 =-1 ){
Controlsubmit = false;
Alert ("Time Format: 2006-09-03 ")
Return;
}
If (strmonth. length! = 2 | month <1 | month> 12 ){
Controlsubmit = false;
Alert ("enter a valid month, for example, 02 ")
Return;
}
If (strday. length! = 2 | day <1 | day> 31 | (month = 2 & day> daysinfebruary (year) | day> daysinmonth [month]) {
Controlsubmit = false;
Alert ("enter a valid day, for example, 02 ")
Return;
}
If (stryear. length! = 4 | year = 0 | year <minyear | year> maxyear ){
Controlsubmit = false;
Alert ("Please enter a 4-digit year between" + minyear + "and" + maxyear + ")
Return;
}
If (dtstr. indexof (dtch, pos2 + 1 )! =-1 | isinteger (stripcharsinbag (dtstr, dtch) = false ){
Controlsubmit = false;
Alert ("enter a valid time format, for example, 2006-09-03 ")
Return;
}
Controlsubmit = true;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.