001 <SCRIPT LANGUAGE="JScript" RUNAT="SERVER">
002 /* ================================================================
003 FUNCTION: isCreditCard(st)
004
005 INPUT: st - a string representing a credit card number
006
007 RETURNS: true, if the credit card number passes the Luhn Mod-10
008 test.
009 false, otherwise
010 ================================================================ */
011
012 function isCreditCard(st) {
013
014 if (st.length > 19)
015 return (false);
016
017 sum = 0; mul = 1; l = st.length;
018 for (i = 0; i < l; i++) {
019 digit = st.substring(l-i-1,l-i);
020 tproduct = parseInt(digit ,10)*mul;
021 if (tproduct >= 10)
022 sum += (tproduct % 10) + 1;
023 else
024 sum += tproduct;
025 if (mul == 1)
026 mul++;
027 else
028 mul--;
029 }
030
031 if ((sum % 10) == 0)
032 return (true);
033 else
034 return (false);
035
036 }
037
038
039
040 /* ================================================================
041 FUNCTION: isVisa()
042
043 INPUT: cc - a string representing a credit card number
044
045 RETURNS: true, if the credit card number is a valid VISA number.
046
047 false, otherwise
048
049 Sample number: 4111 1111 1111 1111 (16 digits)
050 ================================================================ */
051
052 function isVisa(cc)
053 {
054 if (((cc.length == 16) || (cc.length == 13)) &&
055 (cc.substring(0,1) == 4))
056 return isCreditCard(cc);
057 return false;
058 }
059
060
061
062
063 /* ================================================================
064 FUNCTION: isMasterCard()
065
066 INPUT: cc - a string representing a credit card number
067
068 RETURNS: true, if the credit card number is a valid MasterCard
069 number.
070
071 false, otherwise
072
073 Sample number: 5500 0000 0000 0004 (16 digits)
074 ================================================================ */
075
076 function isMasterCard(cc)
077 {
078 firstdig = cc.substring(0,1);
079 seconddig = cc.substring(1,2);
080 if ((cc.length == 16) && (firstdig == 5) &&
081 ((seconddig >= 1) && (seconddig <= 5)))
082 return isCreditCard(cc);
083 return false;
084
085 }
086
087
088
089
090
091 /* ================================================================
092 FUNCTION: isAmericanExpress()
093
094 INPUT: cc - a string representing a credit card number
095
096 RETURNS: true, if the credit card number is a valid American
097 Express number.
098
099 false, otherwise
100
101 Sample number: 340000000000009 (15 digits)
102 ================================================================ */
103
104 function isAmericanExpress(cc)
105 {
106 firstdig = cc.substring(0,1);
107 seconddig = cc.substring(1,2);
108 if ((cc.length == 15) && (firstdig == 3) &&
109 ((seconddig == 4) || (seconddig == 7)))
110 return isCreditCard(cc);
111 return false;
112
113 }
114
115
116
117
118 /* ================================================================
119 FUNCTION: isDinersClub()
120
121 INPUT: cc - a string representing a credit card number
122
123 RETURNS: true, if the credit card number is a valid Diner
124 Club number.
125
126 false, otherwise
127
128 Sample number: 30000000000004 (14 digits)
129 ================================================================ */
130
131 function isDinersClub(cc)
132 {
133 firstdig = cc.substring(0,1);
134 seconddig = cc.substring(1,2);
135 if ((cc.length == 14) && (firstdig == 3) &&
136 ((seconddig == 0) || (seconddig == 6) || (seconddig == 8)))
137 return isCreditCard(cc);
138 return false;
139 }
140
141
142
143 /* ================================================================
144 FUNCTION: isCarteBlanche()
145
146 INPUT: cc - a string representing a credit card number
147
148 RETURNS: true, if the credit card number is a valid Carte
149 Blanche number.
150
151 false, otherwise
152 ================================================================ */
153
154 function isCarteBlanche(cc)
155 {
156 return isDinersClub(cc);
157 }
158
159
160
161
162 /* ================================================================
163 FUNCTION: isDiscover()
164
165 INPUT: cc - a string representing a credit card number
166
167 RETURNS: true, if the credit card number is a valid Discover
168 card number.
169
170 false, otherwise
171
172 Sample number: 6011000000000004 (16 digits)
173 ================================================================ */
174
175 function isDiscover(cc)
176 {
177 first4digs = cc.substring(0,4);
178 if ((cc.length == 16) && (first4digs == "6011"))
179 return isCreditCard(cc);
180 return false;
181
182 }
183
184
185
186
187
188 /* ================================================================
189 FUNCTION: isEnRoute()
190
191 INPUT: cc - a string representing a credit card number
192
193 RETURNS: true, if the credit card number is a valid enRoute
194 card number.
195
196 false, otherwise
197
198 Sample number: 201400000000009 (15 digits)
199 ================================================================ */
200
201 function isEnRoute(cc)
202 {
203 first4digs = cc.substring(0,4);
204 if ((cc.length == 15) &&
205 ((first4digs == "2014") ||
206 (first4digs == "2149")))
207 return isCreditCard(cc);
208 return false;
209 }
210
211
212
213 /* ================================================================
214 FUNCTION: isJCB()
215
216 INPUT: cc - a string representing a credit card number
217
218 RETURNS: true, if the credit card number is a valid JCB
219 card number.
220
221 false, otherwise
222 ================================================================ */
223
224 function isJCB(cc)
225 {
226 first4digs = cc.substring(0,4);
227 if ((cc.length == 16) &&
228 ((first4digs == "3088") ||
229 (first4digs == "3096") ||
230 (first4digs == "3112") ||
231 (first4digs == "3158") ||
232 (first4digs == "3337") ||
233 (first4digs == "3528")))
234 return isCreditCard(cc);
235 return false;
236
237 }
238
239
240
241 /* ================================================================
242 FUNCTION: isAnyCard()
243
244 INPUT: cc - a string representing a credit card number
245
246 RETURNS: true, if the credit card number is any valid credit
247 card number for any of the accepted card types.
248
249 false, otherwise
250 ================================================================ */
251
252 function isAnyCard(cc)
253 {
254 if (!isCreditCard(cc))
255 return false;
256 if (!isMasterCard(cc) && !isVisa(cc) && !isAmericanExpress(cc) && !isDinersClub(cc) &&
257 !isDiscover(cc) && !isEnRoute(cc) && !isJCB(cc)) {
258 return false;
259 }
260 return true;
261
262 }
263
264
265
266 /* ================================================================
267 FUNCTION: isCardMatch()
268
269 INPUT: cardType - a string representing the credit card type
270 cardNumber - a string representing a credit card number
271
272 RETURNS: true, if the credit card number is valid for the particular
273 credit card type given in "cardType".
274
275 false, otherwise
276 ================================================================ */
277
278 function isCardMatch (cardType, cardNumber)
279 {
280
281 cardType = cardType.toUpperCase();
282 var doesMatch = true;
283
284 if ((cardType == "VISA") && (!isVisa(cardNumber)))
285 doesMatch = false;
286 if ((cardType == "MASTERCARD") && (!isMasterCard(cardNumber)))
287 doesMatch = false;
288 if ( ( (cardType == "AMERICANEXPRESS") || (cardType == "AMEX") )
289 && (!isAmericanExpress(cardNumber))) doesMatch = false;
290 if ((cardType == "DISCOVER") && (!isDiscover(cardNumber)))
291 doesMatch = false;
292 if ((cardType == "JCB") && (!isJCB(cardNumber)))
293 doesMatch = false;
294 if ((cardType == "DINERS") && (!isDinersClub(cardNumber)))
295 doesMatch = false;
296 if ((cardType == "CARTEBLANCHE") && (!isCarteBlanche(cardNumber)))
297 doesMatch = false;
298 if ((cardType == "ENROUTE") && (!isEnRoute(cardNumber)))
299 doesMatch = false;
300 return doesMatch;
301
302 }
303 </SCRIPT>