datevalidator.lzx

<!--
********************************************************
* [[validators/datevalidator]]
* 2005/12/21 ver.2.0.0
* 2005/12/06 ver.1.0.1 add trim attribute [togawa manabu]
*
* 2005/11/06 ver.1.0. release
* togawa manabu
* web:http://laszlo.jp
* mail:webmaster@laszlo.jp 
********************************************************
-->
<!---
      @topic Incubator
      @subtopic Validators
  -->
<library>
    <include href="basevalidator.lzx"/>
    
    <class name="datevalidator" extends="basevalidator">
        <attribute name="format" type="string" value="mm/dd/yyyy"/>
        <attribute name="trim" type="boolean" value="true"/>
        
        <attribute name="invalidformatErrorstring" type="string" value="Please input the date in a valid format."/>
        <attribute name="invalidyearErrorstring" type="string" value="Please enter a year between 0 and 9999."/>
        <attribute name="invalidmonthErrorstring" type="string" value="Please enter a month between 1 and 12."/>
        <attribute name="invaliddateErrorstring" type="string" value="Please enter a valid date."/>
        
        <method name="doValidation" args="val">
            

            var valtext = getValueText(val);

            if(this.required && valtext.length < 1){
                this.setErrorstring(this.requiredErrorstring);
                return false;
            }
            if(valtext != ""){
                if(!isValidLength(valtext)){
                    this.setErrorstring(this.invalidformatErrorstring);
                    return false;
                }
                if(!isValidYear(valtext)){
                    this.setErrorstring(this.invalidyearErrorstring);
                    return false;
                }
                if(!isValidMonth(valtext)){
                    this.setErrorstring(this.invalidmonthErrorstring);
                    return false;
                }
                if(!isValidDate(valtext)){
                    this.setErrorstring(this.invaliddateErrorstring);
                    return false;
                }
            }

            
            this.setErrorstring("");
            return true;
        
        </method>
        
        <method name="isValidLength" args="value">
            if(this.format.length == value.length)
                return true;
            else
                return false;
        </method>
        
        <method name="isValidYear" args="value">
        
            var year = getYear(value);
            if(isInt(year)){
                var intYear = parseInt(year, 10);
                if( -1 < intYear && intYear < 10000)
                    return true;  
            }
            
            return false;
            
        </method>
        <method name="isValidMonth" args="value">
        
            var month = getMonth(value);
            if(isInt(month)){
                var intMonth = parseInt(month, 10);
                if(0 < intMonth && intMonth < 13)
                    return true;
            }
            
            return false;
            
        </method>
        <method name="isValidDate" args="value">
        
            var date = getDate(value);
            if(isInt(date)){
                var intDate = parseInt(date, 10);
                if(0 < intDate&& intDate <= getlastdate(value))
                    return true;
            }
            
            return false;
            
        </method>
        <method name="getlastdate" args="value">
            switch (parseInt(getMonth(value), 10)){
                case  2:
                  return getlastdayofFeb(value);
                case  4: case  6: case  9: case 11:
                  return 30;
                default:
                  return 31;
              }
        </method>
        <method name="getlastdayofFeb" args="value">
        
            var year = parseInt(getYear(value), 10);
            if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                return 29;
            else
                return 28;
            
        </method>
        
        <method name="isInt" args="value">
            return ! isNaN(value);
        </method>
        
        <method name="getYear" args="value">
            var idx = this.format.indexOf("yyyy");
            return value.substring(idx, idx+4);
        </method>
        <method name="getMonth" args="value">
            var idx = this.format.indexOf("mm")
            return value.substring(idx, idx+2);
        </method>
        <method name="getDate" args="value">
            var idx = this.format.indexOf("dd")
            return value.substring(idx, idx+2);
        </method>
    </class>
</library>
<!-- * X_LZ_COPYRIGHT_BEGIN ***************************************************
* Copyright 2006-2008 Laszlo Systems, Inc. All Rights Reserved.               *
* Use is subject to license terms.                                            *
* X_LZ_COPYRIGHT_END ****************************************************** -->
<!-- @LZX_VERSION@                                                         -->

Cross References

Includes

Classes