Javascript date Verification

Source: Internet
Author: User

// Get the array date_array by the page element date_string (YYYY-MM_DD format)
// Input parameter: date_string: Date string; marker: Separator in date_string. If not, it is "";
// Return value: array: date_array
// Array element date_array.year = yyyy; date_array.month = mm; date_array.day = dd; date_array.datestring = yyyymmdd
Function date_get (date_string, marker)
{
VaR date_array = new array ()
If (date_string.length! = 0)
{
If (marker! = "") // If the input string has a separator
{
VaR year = date_string.substr (0, 4 );
VaR month = date_string.substr (5, 2 );
VaR day = date_string.substr (8, 2 );
}
Else // If the input string does not have a separator
{
VaR year = date_string.substr (0, 4 );
VaR month = date_string.substr (4, 2 );
VaR day = date_string.substr (6, 2 );
}

Date_array.year = year;
Date_array.month = month;
Date_array.day = Day;
Date_array.datestring = year + month + day + "";
Return date_array;
}

Date_array.year = "";
Date_array.month = "";
Date_array.day = "";
Date_array.datestring = "";

Return date_array;
}

// Year must be greater than 1900; month must be between 1 and 12; day must be between 1 and 31
// Enter the name of the control label to be displayed in the year, month, day, and error message.
// Return value: array: check_result
// Array element check_result.status: True/false: Check successful/failed;
// Array element check_result.message: ""/empty string of error message (when verification is successful)/error message (when verification fails );
Function date_check (date_array, field_name)
{
VaR check_result = new array ();

// Obtain the number. (Page)
VaR year = date_array.year;
VaR month = date_array.month;
VaR day = date_array.day;
VaR hour = date_array.hour;
VaR minute = date_array.minute;
VaR second = date_array.second;

// The year must be YYYY (use a regular expression to determine if it is a number)
VaR err_message = 'date must conform to the YYYY-MM-DD format, and the year must consist of four digits in the yyyy format! ';
// Var year_patten =/^ [0-9] {4} $/I;
VaR year_patten =/^/d + $/I;
VaR matcharray = year. Match (year_patten );
If (Year. length! = 0 & matcharray = NULL)
{
Check_result.status = false;
Check_result.message = err_message;
Return check_result;
}

// The month must be mm (use a regular expression to determine if it is a number)
VaR err_message = 'date must conform to the YYYY-MM-DD format, and the month must consist of two digits in mm format! ';
// Var month_patten =/^ [0-9] {2, 2} $/I;
VaR month_patten =/^/d + $/I;
VaR matcharray = month. Match (month_patten );
If (month. length! = 0 & matcharray = NULL)
{
Check_result.status = false;
Check_result.message = err_message;
Return check_result;
}
// The date must be dd (use a regular expression to determine whether it is a number)
VaR err_message = 'date must conform to the YYYY-MM-DD format, and date must consist of two digits in the DD format! ';
// Var day_patten =/^ [0-9] {2, 2} $/I;
VaR day_patten =/^/d + $/I;
VaR matcharray = day. Match (day_patten );
If (day. length! = 0 & matcharray = NULL)
{
Check_result.status = false;
Check_result.message = err_message;
Return check_result;
}

/* Alert ("OK ");
Alert (hour );

// The date must be dd (use a regular expression to determine whether it is a number)
VaR err_message = 'date must conform to the YYYY-MM-DD format, hour must be composed of two digits in HH format! ';
VaR hour_patten =/^ [0-9] {2, 2} $/I;
// Var hour_patten =/^/d + $/I;
VaR matcharray = hour. Match (hour_patten );
If (hour. length! = 0 & matcharray = NULL)
{
Check_result.status = false;
Check_result.message = err_message;
Return check_result;
}

// The date must be dd (use a regular expression to determine whether it is a number)
VaR err_message = 'date must conform to the YYYY-MM-DD format, minutes must be composed of two digits in mm format! ';
VaR minute_patten =/^ [0-9] {2, 2} $/I;
// Var minute_patten =/^/d + $/I;
VaR matcharray = minute. Match (minute_patten );
If (minute. length! = 0 & matcharray = NULL)
{
Check_result.status = false;
Check_result.message = err_message;
Return check_result;
}

// The date must be dd (use a regular expression to determine whether it is a number)
VaR err_message = 'date must conform to the YYYY-MM-DD format, and date must consist of two digits in SS format! ';
VaR second_patten =/^ [0-9] {2, 2} $/I;
// Var second_patten =/^/d + $/I;
VaR matcharray = second. Match (second_patten );
If (second. length! = 0 & matcharray = NULL)
{
Check_result.status = false;
Check_result.message = err_message;
Return check_result;
}
*/

VaR year_int = parseint (year, 10 );
VaR month_int;
VaR day_int;

If (month. substr (0, 1) = "0 ")
Month_int = parseint (month. substr (1, 1 ));
Else
Month_int = parseint (month );

If (day. substr (0, 1) = "0 ")
Day_int = parseint (day. substr (1, 1 ));
Else
Day_int = parseint (day );

If (year_int % 4 = 0) & (year_int % 100! = 0) | (year_int % 400 = 0 ))
{
VaR day_number = new array (, 30, 31, 30, 31 );
}
Else
{
VaR day_number = new array (, 30, 31, 30, 31 );
}
If (year_int <1, 1900)
{
The year of VaR err_message = field_name + "is meaningless! Enter again! ";

Check_result.status = false;
Check_result.message = err_message;
Return check_result;
}
If (month_int <1 | month_int> 12)
{
The month of VaR err_message = field_name + "is incorrect! Make sure it meets the 'yyyy-MM-DD 'format! ";

Check_result.status = false;
Check_result.message = err_message;
Return check_result;
}
If (day_int <1 | day_int> day_number [(month-1)])
{
The date section of VaR err_message = field_name + "is incorrect! Make sure it meets the 'yyyy-MM-DD 'format! ";

Check_result.status = false;
Check_result.message = err_message;
Return check_result;
}
If (hour <0 | hour> 24)
{
The hour section of VaR err_message = field_name + "is incorrect! Make sure it meets the 'yyyy-MM-DD 'format! ";

Check_result.status = false;
Check_result.message = err_message;
Return check_result;
}
If (minute <0 | minute> 60)
{
The minute section of VaR err_message = field_name + "is incorrect! Make sure it meets the 'yyyy-MM-DD 'format! ";

Check_result.status = false;
Check_result.message = err_message;
Return check_result;
}
If (second <0 | second> 60)
{
The second part of VaR err_message = field_name + "is incorrect! Make sure it meets the 'yyyy-MM-DD 'format! ";

Check_result.status = false;
Check_result.message = err_message;
Return check_result;
}

Check_result.status = true;
Check_result.message = "";
Return check_result;
}

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/taojiahuanjia/archive/2008/08/22/2814696.aspx

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.