ValidationUtil

Kind of class: public class
Package:
Inherits from:
  • none
Version: 05/06/11
Author: Aaron Clinger
Classpath: org.casalib.util.ValidationUtil
File last modified: Friday, 20 May 2011, 00:59:45
► View source▼ Hide source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
    CASA Lib for ActionScript 3.0
    Copyright (c) 2011, Aaron Clinger & Contributors of CASA Lib
    All rights reserved.
     
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
     
    - Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
     
    - Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
     
    - Neither the name of the CASA Lib nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.
     
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/
package org.casalib.util {
    import org.casalib.util.ArrayUtil;
     
    /**
        Utilities for validating common string formats.
         
        @author Aaron Clinger
        @version 05/06/11
    */
    public class ValidationUtil {
         
         
        /**
            Determines if String is a valid email address.
             
            @param email: String to verify as email.
            @return Returns <code>true</code> if String is a valid email; otherwise <code>false</code>.
            @see <a href="http://www.regular-expressions.info/email.html">Read more about the regular expression used by this method.</a>
        */
        public static function isEmail(email:String):Boolean {
            const pattern:RegExp = /^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
            return email.match(pattern) != null;
        }
         
        /**
            Determines if String is a valid USA state abbreviation.
             
            @param state: String to verify as two letter state abbreviation (includes DC).
            @return Returns <code>true</code> if String is a state abbreviation; otherwise <code>false</code>.
        */
        public static function isUsaStateAbbreviation(state:String):Boolean {
            const states:Array = new Array('ak', 'al', 'ar', 'az', 'ca', 'co', 'ct', 'dc', 'de', 'fl', 'ga', 'hi', 'ia', 'id', 'il', 'in', 'ks', 'ky', 'la', 'ma', 'md', 'me', 'mi', 'mn', 'mo', 'ms', 'mt', 'nb', 'nc', 'nd', 'nh', 'nj', 'nm', 'nv', 'ny', 'oh', 'ok', 'or', 'pa', 'ri', 'sc', 'sd', 'tn', 'tx', 'ut', 'va', 'vt', 'wa', 'wi', 'wv', 'wy');
            return ArrayUtil.contains(states, state.toLowerCase()) == 1;
        }
         
        /**
            Determines if the date provided is equal to or greater than a certain age.
             
            @param age: The age to validate.
            @param yearBorn: The year of birth.
            @param monthBorn: The month of birth, <code>0 </code> for January to <code>11</code> for December.
            @param dateBorn: The day of the month of birth, from <code>1</code> to <code>31</code>.
            @return Returns <code>true</code> if the date provided is equal to or greater than the age; otherwise <code>false</code>.
        */
        public static function isAge(age:uint, yearBorn:uint, monthBorn:uint = 0, dateBorn:uint = 1):Boolean {
            const currentDate:Date = new Date();
             
            if (yearBorn > currentDate.getFullYear() - age)
                return false;
             
            if (yearBorn < currentDate.getFullYear() - age)
                return true;
             
            if (monthBorn > currentDate.getMonth())
                return false;
             
            if (monthBorn < currentDate.getMonth())
                return true;
             
            if (dateBorn <= currentDate.getDate())
                return true;
             
            return false;
        }
         
        /**
            Determines if credit card is valid using the Luhn formula.
             
            @param cardNumber: The credit card number.
            @return Returns <code>true</code> if String is a valid credit card number; otherwise <code>false</code>.
        */
        public static function isCreditCard(cardNumber:String):Boolean {
            if (cardNumber.length < 7 || cardNumber.length > 19 || Number(cardNumber) < 1000000)
                return false;
             
            var sum:Number  = 0;
            var alt:Boolean = true;
            var i:Number    = cardNumber.length;
            var pre:Number;
             
            while (--i > -1) {
                if (alt)
                    sum += Number(cardNumber.substr(i, 1));
                else {
                    pre =  Number(cardNumber.substr(i, 1)) * 2;
                    sum += (pre > 8) ? pre -= 9 : pre;
                }
                 
                alt = !alt;
            }
             
            return sum % 10 == 0;
        }
         
        /**
            Determines credit card provider by card number.
             
            @param cardNumber: The credit card number.
            @return Returns name of the provider; values can be <code>"visa"</code>, <code>"mastercard"</code>, <code>"discover"</code>, <code>"amex"</code>, <code>"diners"</code>, <code>"other"</code> or <code>"invalid"</code>.
        */
        public static function getCreditCardProvider(cardNumber:String):String {
            if (!ValidationUtil.isCreditCard(cardNumber))
                return 'invalid';
             
            if (cardNumber.length == 13 ||
                cardNumber.length == 16 &&
                cardNumber.indexOf('4') == 0)
            {
                return 'visa';
            }
            else if (cardNumber.indexOf('51') == 0 ||
                     cardNumber.indexOf('52') == 0 ||
                     cardNumber.indexOf('53') == 0 ||
                     cardNumber.indexOf('54') == 0 ||
                     cardNumber.indexOf('55') == 0 &&
                     cardNumber.length == 16)
            {
                return 'mastercard';
            }
            else if (cardNumber.length == 16 &&
                     cardNumber.indexOf('6011') == 0)
            {
                 return 'discover';
            }
            else if (cardNumber.indexOf('34') == 0 ||
                     cardNumber.indexOf('37') == 0 &&
                     cardNumber.length == 15)
            {
                return 'amex';
            }
            else if (cardNumber.indexOf('300') == 0 ||
                     cardNumber.indexOf('301') == 0 ||
                     cardNumber.indexOf('302') == 0 ||
                     cardNumber.indexOf('303') == 0 ||
                     cardNumber.indexOf('304') == 0 ||
                     cardNumber.indexOf('305') == 0 ||
                     cardNumber.indexOf('36') == 0 ||
                     cardNumber.indexOf('38') == 0 &&
                     cardNumber.length == 14)
            {
                return 'diners';
            }
            else return 'other';
        }
    }
}
Utilities for validating common string formats.

Summary

Class methods
  • isEmail (email:String) : Boolean
    • Determines if String is a valid email address.
  • isUsaStateAbbreviation (state:String) : Boolean
    • Determines if String is a valid USA state abbreviation.
  • isAge (age:uint, yearBorn:uint, monthBorn:uint, dateBorn:uint = 1) : Boolean
    • Determines if the date provided is equal to or greater than a certain age.
  • isCreditCard (cardNumber:String) : Boolean
    • Determines if credit card is valid using the Luhn formula.
  • getCreditCardProvider (cardNumber:String) : String
    • Determines credit card provider by card number.

Class methods

getCreditCardProvider

static function getCreditCardProvider(cardNumber:String) : String

Determines credit card provider by card number.

Parameters
cardNumber:The credit card number.
Returns
  • Returns name of the provider; values can be "visa", "mastercard", "discover", "amex", "diners", "other" or "invalid".

isAge

static function isAge(age:uint, yearBorn:uint, monthBorn:uint, dateBorn:uint = 1) : Boolean

Determines if the date provided is equal to or greater than a certain age.

Parameters
age :The age to validate.
yearBorn :The year of birth.
monthBorn:The month of birth, 0 for January to 11 for December.
dateBorn :The day of the month of birth, from 1 to 31.
Returns
  • Returns true if the date provided is equal to or greater than the age; otherwise false.

isCreditCard

static function isCreditCard(cardNumber:String) : Boolean

Determines if credit card is valid using the Luhn formula.

Parameters
cardNumber:The credit card number.
Returns
  • Returns true if String is a valid credit card number; otherwise false.

isEmail

static function isEmail(email:String) : Boolean

Determines if String is a valid email address.

Parameters
email:String to verify as email.
Returns
  • Returns true if String is a valid email; otherwise false.
See also

isUsaStateAbbreviation

static function isUsaStateAbbreviation(state:String) : Boolean

Determines if String is a valid USA state abbreviation.

Parameters
state:String to verify as two letter state abbreviation (includes DC).
Returns
  • Returns true if String is a state abbreviation; otherwise false.