Open-Source PHP Framework - Designed for rapid development of performance-oriented scalable applications

/webroot/js/validator.js

[return to app]
1 <?php //this file needs to be parsed as PHP before being sent to the client
2 /**
3  * This comes from the May 2007 php|architect article:
4  * Unifying Server-Side and Client-Side Input Validation
5  * @link http://www.Vork.us
6  */
7
header('Content-type: application:x-javascript');
8
9 require_once 
'../../packages/validator';
10
$Validator = new Validator;
11
12 echo 
'var commonRegex = ' $Validator->getCommonData('commonRegex') . ';' PHP_EOL;
13 echo 
'var validCssColors = ' json_encode($Validator->getValidCssColors()) . ';' PHP_EOL;
14
?>
15
function Validator() {}
16
17 for (var key in commonRegex.match) {
18     eval('Validator.isValid' + key + ' = function(val) {' +
19     '    return commonRegex["match"]["' + key + '"].test(val);' +
20     '}');
21 }
22
23 Validator.dom = function(id) {
24     var retval;
25     if (typeof document.getElementById != 'undefined') {
26         retval = document.getElementById(id);
27     } else if (typeof document.all != 'undefined') {
28         retval = document.all[id];
29     } else {
30         retval = false;
31     }
32     return retval;
33 }
34
35 Validator.getNumbersOnly = function(val, integerOnly) {
36     var regexType = (typeof integerOnly == 'undefined' || !integerOnly ? 'Numeric' : 'Integer');
37     var matches = val.match(commonRegex['get'][regexType]);
38     var numbers = '';
39     for (var match in matches) {
40         numbers += matches[match];
41     }
42     return numbers;
43 }
44
45 Validator.isValidTelephone = function(val, country) {
46     var retval;
47     if (typeof country == 'undefined' || country.toUpperCase() == 'US') {
48         retval = commonRegex['match']['UsTelephone'].test(val);
49     } else {
50         retval = (Validator.getNumbersOnly(val).length > 6);
51     }
52     return retval;
53 }
54
55 Validator.isValidHexColorParent = Validator.isValidHexColor;
56 Validator.isValidHexColor = function(val) {
57     if (typeof validCssColors[val] != 'undefined') {
58         val = '#' + validCssColors[val];
59     }
60     return Validator.isValidHexColorParent(val);
61 }
62
63 Validator.getTrim = function(val) {
64     return val.replace(commonRegex['get']['Trim'], '$1');
65 }
66
67 Validator.validateForm = function(formId) {
68     var errors = new Array;
69     var obj;
70     for (var inputId in validations[formId]) {
71         obj = Validator.dom(inputId);
72         if (obj) {
73             obj.value = Validator.getTrim(obj.value);
74         }
75
76         if (!obj || !Validator[validations[formId][inputId]](obj.value)) {
77             errors[errors.length] = inputId;
78         } else {
79             Validator.dom(inputId + 'errorwrapper').innerHTML = '';
80         }
81     }
82
83     if (errors.length) {
84         var label = '';
85         var errorsLength = errors.length;
86         for (var x = 0; x < errorsLength; x++) {
87             label = Validator.dom(errors[x] + 'label').innerHTML;
88             //uncomment the next line if you consistently append a colon to your labels
89             //label = label.substr(0, (label.length - 1));
90             Validator.dom(errors[x] + 'errorwrapper').innerHTML = label + ' is not valid';
91             if (!x) {
92                 Validator.dom(errors[x]).focus();
93             }
94         }
95     }
96     return !errors.length;
97 }