日期:2014-05-16  浏览次数:20398 次

JavaScript日期验证

? /*
?*validate date(include bissextile)
?*format: dd-mm-yyyy
?*add by tony
?*/
?function isValidDate(sText) {
???? //yyyy-MM-dd
???? var reDate = /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29))$/
???? //dd-mm-yyyy
???? //var reDate = /^(((0?[1-9]|[12]\d|3[01])-(0?[13578]|1[02])-((1[6-9]|[2-9]\d)\d{2}))|((0?[1-9]|[12]\d|30)-(0?[13456789]|1[012])-((1[6-9]|[2-9]\d)\d{2}))|((0?[1-9]|1\d|2[0-8])-0?2-((1[6-9]|[2-9]\d)\d{2}))|(29-0?2-((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/
???? return reDate.test(sText);
?}
?function validateDate(obj){
??alert(""+document.proDocForm.beginDate.value);
??? ?var begin = trim(obj.value);
??? ?if( begin != ""){
???if(isValidDate(begin) == false){
???????????? alert("请你输入正确的日期格式:YYYY-MM-DD"); ????? ?
????? ?obj.value="";
????? ?obj.focus();
???????? }
??? ?}
?} ?
?
?function ltrim(s){
??return s.replace( /^\s*/, "");
?}
?//去右空格;
?function rtrim(s){
??return s.replace( /\s*$/, "");
?}
?//左右空格;
?function trim(s){
??return rtrim(ltrim(s));
?}