- package yiyou
- {
- import mx.controls.Alert;
- public class GenDateValidator
- {
- public function GenDateValidator()
- {
- // validatorDate(str);
- }
- public function validatorDate(str:String):Boolean
- {
- var formatStr:RegExp=/^[1-9][0-9][0-9][0-9]年[0-1][0-9]月[0-3][0-9]日[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$/;
- var flag:Boolean = false;
- /**
- * 验证输入格式是否合法
- * 验证的内容有格式是否为
- * 1:xxxx年xx月xx日xx:xx:xx
- * 2:月份是否为
- **/
- if(formatStr.exec(str)==null){
- Alert.show("输入格式不合法");
- }else
- {
- //当小时为24小时的时候,分钟和秒钟都要为00
- var yearStr:String = str.substr(0,4);
- var monthStr:String = str.substr(5,2);
- var dayStr:String = str.substr(8,2);
- var hourStr:String=str.substr(11,2);//截取小时
- var minStr:String = str.substr(14,2);
- var secondStr:String = str.substr(17,2);
- var year:Number = parseInt(yearStr);
- var month:Number = parseInt(monthStr);
- var day:Number = parseInt(dayStr);
- var hour:Number = parseInt(hourStr);
- var minute:Number = parseInt(minStr);
- var second:Number = parseInt(secondStr);
- flag = (validatorMonth(month) && validatorMonth(month) && validatorDay(day,month,year) && validatorHour(hour,minute,second));
- }
- return flag;
- }
- public function validatorMonth(month:Number):Boolean{
- //月份必须小于12,
- if(month>12)
- {
- Alert.show("月份必须小于12");
- return false;
- }
- return true;
- }
- public function validatorDay(day:Number,month:Number,year:Number):Boolean{
- //日期必须小于31,如果是4,6,8,10,必须小于30,如果是闰年,2月份必须小于29
- if(day>31)
- {
- Alert.show("日份必须小于31");
- return false;
- }else
- {
- if(month == 4||month == 6||month == 9||month == 10)
- {
- if(day>30)
- {
- Alert.show("日份必须小于30");
- return false;
- }
- }
- if(month == 2)
- {
- //判断选择的年是否为闰年
- if((year%4==0 && year%100!=0)||(year%400==0))
- {
- if(day>29)
- {
- Alert.show("2月份必须小于29");
- return false;
- }
- }else
- {
- if(day>28)
- {
- Alert.show("平年2月份必须小于28");
- return false;
- }
- }
- }
- }
- return true;
- }
- public function validatorHour(hour:Number,minute:Number,second:Number):Boolean{
- //小时必须小于24,且当小时为24时,分钟和秒钟都应该是0
- if(hour>24)
- {
- Alert.show("小时必须小于或者等于24");
- return false;
- }else if(hour==24)
- {
- if(minute!=00 || second !=00)
- {
- Alert.show("当小时为24时,分秒必须都是0");
- return false;
- }
- }
- return true;
- }
- }
- }
package yiyou
{
import mx.controls.Alert;
public class GenDateValidator
{
public function GenDateValidator()
{
// validatorDate(str);
}
public function validatorDate(str:String):Boolean
{
var formatStr:RegExp=/^[1-9][0-9][0-9][0-9]年[0-1][0-9]月[0-3][0-9]日[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$/;
var flag:Boolean = false;
/**
* 验证输入格式是否合法
* 验证的内容有格式是否为
* 1:xxxx年xx月xx日xx:xx:xx
* 2:月份是否为
**/
if(formatStr.exec(str)==null){
Alert.show("输入格式不合法");
}else
{
//当小时为24小时的时候,分钟和秒钟都要为00
var yearStr:String = str.substr(0,4);
var monthStr:String = str.substr(5,2);
var dayStr:String = str.substr(8,2);
var hourStr:String=str.substr(11,2);//截取小时
var minStr:String = str.substr(14,2);
var secondStr:String = str.substr(17,2);
var year:Number = parseInt(yearStr);
var month:Number = parseInt(monthStr);
var day:Number = parseInt(dayStr);
var hour:Number = parseInt(hourStr);
var minute:Number = parseInt(minStr);
var second:Number = parseInt(secondStr);
flag = (validatorMonth(month) && validatorMonth(month) && validatorDay(day,month,year) && validatorHour(hour,minute,second));
}
return flag;
}
public function validatorMonth(month:Number):Boolean{
//月份必须小于12,
if(month>12)
{
Alert.show("月份必须小于12");
return false;
}
return true;
}
public function validatorDay(day:Number,month:Number,year:Number):Boolean{
//日期必须小于31,如果是4,6,8,10,必须小于30,如果是闰年,2月份必须小于29
if(day>31)
{
Alert.show("日份必须小于31");
return false;
}else
{
if(month == 4||month == 6||month == 9||month == 10)
{
if(day>30)
{
Alert.show("日份必须小于30");
return false;
}
}
if(month == 2)
{
//判断选择的年是否为闰年
if((year%4==0 && year%100!=0)||(year%400==0))
{
if(day>29)
{
Alert.show("2月份必须小于29");
return false;
}
}else
{
if(day>28)
{
Alert.show("平年2月份必须小于28");
return false;
}
}
}
}
return true;
}
public function validatorHour(hour:Number,minute:Number,second:Number):Boolean{
//小时必须小于24,且当小时为24时,分钟和秒钟都应该是0
if(hour>24)
{
Alert.show("小时必须小于或者等于24");
return false;
}else if(hour==24)
{
if(minute!=00 || second !=00)
{
Alert.show("当小时为24时,分秒必须都是0");
return false;
}
}
return true;
}
}
}
测试代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
- xmlns:s="library://ns.adobe.com/flex/spark"
- xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:yiyou="yiyou.*">
- <fx:Script>
- <![CDATA[
- import mx.controls.Alert;
- import yiyou.GenDateValidator;
- protected function btnID_clickHandler(event:MouseEvent):void
- {
- var str:String=dataFieldID.text;
- var validator:GenDateValidator = new GenDateValidator();
- var flag:Boolean = validator.validatorDate(str);
- if(flag)
- {
- Alert.show("输入合法");
- //处理步骤
- }
- }
- ]]>
- </fx:Script>
- <fx:Declarations>
- <!-- 将非可视元素(例如服务、值对象)放在此处 -->
- </fx:Declarations>
- <yiyou:GenDateField x="239" y="100" id="dataFieldID">
- </yiyou:GenDateField>
- <s:Button id="btnID" x="239" y="168" label="提交" click="btnID_clickHandler(event)"/>
- </s:Application>