1 // Copyright (C) 2005 Rod Roark <rod@sunsetsystems.com>
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
8 // Onkeyup handler for dates. Converts dates that are keyed in to a
9 // consistent format, and helps to reduce typing errors.
11 function datekeyup(e, defcc, withtime) {
12 if (typeof(withtime) == 'undefined') withtime = false;
16 var arr = new Array(0, 0, 0, 0, 0, 0);
20 // Build an array to facilitate error checking.
21 for (var i = 0; i < v.length; ++i) {
23 if (c >= '0' && c <= '9') {
25 } else if (ix < 2 && (c == '-' || c == '/')) {
27 } else if (withtime && ix == 2 && c == ' ') {
29 } else if (withtime && (ix == 3 || ix == 4) && c == ':') {
32 e.value = v.substring(0, i);
37 // We have finished scanning the string. If there is a problem,
38 // drop the last character and repeat the loop.
40 (!withtime && ix > 2) ||
41 (ix > 4 && arr[4] == 0) ||
42 (ix > 3 && arr[3] == 0) ||
43 (ix > 2 && arr[2] == 0) ||
44 (ix > 1 && arr[1] == 0) ||
45 (ix > 0 && arr[0] == 0) ||
47 (ix > 0 && arr[0] > 2 && (arr[0] != 4 || arr[1] > 2 || arr[2] > 2)) ||
48 (arr[2] > 2 && (arr[2] > 4 || arr[0] > 2 || arr[1] > 2)))
50 e.value = v.substring(0, v.length - 1);
56 // The remainder does reformatting if there is enough data for that.
57 if (arr[2] == 4 && defcc == '1') { // mm/dd/yyyy
58 e.value = v.substring(arr[0] + arr[1] + 2, arr[0] + arr[1] + 6) + '-'; // year
59 if (arr[0] == 1) e.value += '0';
60 e.value += v.substring(0, arr[0]) + '-'; // month
61 if (arr[1] == 1) e.value += '0';
62 e.value += v.substring(arr[0] + 1, arr[0] + 1 + arr[1]); // day
64 else if (arr[2] == 4) { // dd-mm-yyyy
65 e.value = v.substring(arr[0] + arr[1] + 2, arr[0] + arr[1] + 6) + '-'; // year
66 if (arr[1] == 1) e.value += '0';
67 e.value += v.substring(arr[0] + 1, arr[0] + 1 + arr[1]) + '-'; // month
68 if (arr[0] == 1) e.value += '0';
69 e.value += v.substring(0, arr[0]); // day
71 else if (arr[0] == 4 && arr[2] > 0) { // yyyy-mm-dd
72 e.value = v.substring(0, arr[0]) + '-'; // year
73 if (arr[1] == 1) e.value += '0';
74 e.value += v.substring(arr[0] + 1, arr[0] + 1 + arr[1]) + '-'; // month
75 e.value += v.substring(arr[0] + arr[1] + 2, arr[0] + arr[1] + 2 + arr[2]); // day (may be 1 digit)
77 else if (arr[0] == 8 && defcc == '1') { // yyyymmdd
78 e.value = v.substring(0, 4) + '-'; // year
79 e.value += v.substring(4, 6) + '-'; // month
80 e.value += v.substring(6, 8); // day
82 else if (arr[0] == 8) { // ddmmyyyy
83 e.value = v.substring(4, 8) + '-'; // year
84 e.value += v.substring(2, 4) + '-'; // month
85 e.value += v.substring(0, 2); // day
91 e.value += v.substring(arr[0] + arr[1] + arr[2] + 2);
95 // Onblur handler to avoid incomplete entry of dates.
97 function dateblur(e, defcc, withtime) {
98 if (typeof(withtime) == 'undefined') withtime = false;
101 if (v.length == 0) return;
103 var arr = new Array(0, 0, 0, 0, 0);
105 for (var i = 0; i < v.length; ++i) {
107 if (c >= '0' && c <= '9') {
109 } else if (c == '-' || c == '/' || c == ' ' || c == ':') {
112 alert('Invalid character in date!');
117 // A birth date may be just age in years, in which case we convert it.
118 if (ix == 0 && arr[0] > 0 && arr[0] <= 3 && e.name.indexOf('DOB') >= 0) {
120 d = new Date(d.getTime() - parseInt(v) * 365.25 * 24 * 60 * 60 * 1000);
121 var s = '' + d.getFullYear() + '-';
122 if (d.getMonth() < 9) s += '0';
123 s += (d.getMonth() + 1) + '-';
124 if (d.getDate() < 10) s += '0';
130 if ((!withtime && ix != 2) || (withtime && ix < 2) || arr[0] != 4 || arr[1] != 2 || arr[2] < 1) {
131 if (confirm('Date entry is incomplete! Try again?'))
139 e.value = v.substring(0, 8) + '0' + v.substring(8);
143 // Private subroutine for US phone number formatting.
144 function usphone(v) {
145 if (v.length > 0 && v.charAt(0) == '-') v = v.substring(1);
146 var oldlen = v.length;
147 for (var i = 0; i < v.length; ++i) {
149 if (c < '0' || c > '9') {
150 v = v.substring(0, i) + v.substring(i + 1);
154 if (oldlen > 3 && v.length >= 3) {
155 v = v.substring(0, 3) + '-' + v.substring(3);
156 if (oldlen > 7 && v.length >= 7) {
157 v = v.substring(0, 7) + '-' + v.substring(7);
158 if (v.length > 12) v = v.substring(0, 12);
164 // Private subroutine for non-US phone number formatting.
165 function nonusphone(v) {
166 for (var i = 0; i < v.length; ++i) {
168 if (c < '0' || c > '9') {
169 v = v.substring(0, i) + v.substring(i + 1);
176 // Telephone country codes that are exactly 2 digits.
177 var twodigitccs = '/20/30/31/32/33/34/36/39/40/41/43/44/45/46/47/48/49/51/52/53/54/55/56/57/58/60/61/62/63/64/65/66/81/82/84/86/90/91/92/93/94/95/98/';
179 // Onkeyup handler for phone numbers. Helps to ensure a consistent
180 // format and to reduce typing errors. defcc is the default telephone
181 // country code as a string.
183 function phonekeyup(e, defcc) {
185 var oldlen = v.length;
187 // Deal with international formatting.
188 if (v.length > 0 && v.charAt(0) == '+') {
190 for (var i = 1; i < v.length; ++i) {
192 if (c < '0' || c > '9') {
193 v = v.substring(0, i) + v.substring(i + i);
198 if (i == 1 && oldlen > 2) {
199 if (cc == '1') { // USA
200 e.value = '+1-' + usphone(v.substring(2));
203 if (cc == '7') { // USSR
204 e.value = '+7-' + nonusphone(v.substring(2));
208 else if (i == 2 && oldlen > 3) {
209 if (twodigitccs.indexOf(cc) >= 0) {
210 e.value = v.substring(0, 3) + '-' + nonusphone(v.substring(3));
214 else if (i == 3 && oldlen > 4) {
215 e.value = v.substring(0, 4) + '-' + nonusphone(v.substring(4));
224 e.value = usphone(v);
226 e.value = nonusphone(v);
232 // onKeyUp handler for mask-formatted fields.
233 // This feature is experimental.
234 function maskkeyup(elem, mask) {
235 if (!mask || mask.length == 0) return;
236 var i = 0; // elem and mask index
238 for (; i < mask.length && i < v.length; ++i) {
239 var ec = v.charAt(i);
240 var mc = mask.charAt(i);
241 if (mc == '#' && (ec < '0' || ec > '9')) {
242 // digit required but this is not one
245 if (mc == '@' && ec.toLowerCase() == ec.toUpperCase()) {
246 // alpha character required but this is not one
250 v = v.substring(0, i);
251 while (i < mask.length) {
252 var mc = mask.charAt(i++);
253 if (mc == '*' || mc == '#' || mc == '@') break;
259 // onBlur handler for mask-formatted fields.
260 // This feature is experimental.
261 function maskblur(elem, mask) {
264 if (i > 0 && v.length > 0 && v.length != i) {
265 // there is a mask and a value but the value is not long enough
266 for (; i > 0 && mask.charAt(i-1) == '#'; --i);
267 // i is now index to first # in # string at end of mask
269 // value is too short even if trailing digits in the mask are ignored
270 if (confirm('Field entry is incomplete! Try again?'))
276 // if the mask ends with digits then right-justify them in the value
277 while (v.length < mask.length) {
278 v = v.substring(0, i) + '0' + v.substring(i, v.length);