2 * $Id: validate.js 65 2006-08-24 15:54:55Z spocke $
\r
4 * Various form validation methods.
\r
7 * @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved.
\r
11 // String validation:
\r
13 if (!Validator.isEmail('myemail'))
\r
14 alert('Invalid email.');
\r
18 var f = document.forms['myform'];
\r
20 if (!Validator.isEmail(f.myemail))
\r
21 alert('Invalid email.');
\r
25 isEmail : function(s) {
\r
26 return this.test(s, '^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$');
\r
29 isAbsUrl : function(s) {
\r
30 return this.test(s, '^(news|telnet|nttp|file|http|ftp|https)://[-A-Za-z0-9\\.]+\\/?.*$');
\r
33 isSize : function(s) {
\r
34 return this.test(s, '^[0-9]+(px|%)?$');
\r
37 isId : function(s) {
\r
38 return this.test(s, '^[A-Za-z_]([A-Za-z0-9_])*$');
\r
41 isEmpty : function(s) {
\r
44 if (s.nodeName == 'SELECT' && s.selectedIndex < 1)
\r
47 if (s.type == 'checkbox' && !s.checked)
\r
50 if (s.type == 'radio') {
\r
51 for (i=0, nl = s.form.elements; i<nl.length; i++) {
\r
52 if (nl[i].type == "radio" && nl[i].name == s.name && nl[i].checked)
\r
59 return new RegExp('^\\s*$').test(s.nodeType == 1 ? s.value : s);
\r
62 isNumber : function(s, d) {
\r
63 return !isNaN(s.nodeType == 1 ? s.value : s) && (!d || !this.test(s, '^-?[0-9]*\\.[0-9]*$'));
\r
66 test : function(s, p) {
\r
67 s = s.nodeType == 1 ? s.value : s;
\r
69 return s == '' || new RegExp(p).test(s);
\r
73 var AutoValidator = {
\r
78 number_cls : 'number',
\r
79 email_cls : 'email',
\r
81 required_cls : 'required',
\r
82 invalid_cls : 'invalid',
\r
87 init : function(s) {
\r
91 this.settings[n] = s[n];
\r
94 validate : function(f) {
\r
95 var i, nl, s = this.settings, c = 0;
\r
97 nl = this.tags(f, 'label');
\r
98 for (i=0; i<nl.length; i++)
\r
99 this.removeClass(nl[i], s.invalid_cls);
\r
101 c += this.validateElms(f, 'input');
\r
102 c += this.validateElms(f, 'select');
\r
103 c += this.validateElms(f, 'textarea');
\r
108 invalidate : function(n) {
\r
109 this.mark(n.form, n);
\r
112 reset : function(e) {
\r
113 var t = new Array('label', 'input', 'select', 'textarea');
\r
114 var i, j, nl, s = this.settings;
\r
119 for (i=0; i<t.length; i++) {
\r
120 nl = this.tags(e.form ? e.form : e, t[i]);
\r
121 for (j=0; j<nl.length; j++)
\r
122 this.removeClass(nl[j], s.invalid_cls);
\r
126 validateElms : function(f, e) {
\r
127 var nl, i, n, s = this.settings, st = true, va = Validator, v;
\r
129 nl = this.tags(f, e);
\r
130 for (i=0; i<nl.length; i++) {
\r
133 this.removeClass(n, s.invalid_cls);
\r
135 if (this.hasClass(n, s.required_cls) && va.isEmpty(n))
\r
136 st = this.mark(f, n);
\r
138 if (this.hasClass(n, s.number_cls) && !va.isNumber(n))
\r
139 st = this.mark(f, n);
\r
141 if (this.hasClass(n, s.int_cls) && !va.isNumber(n, true))
\r
142 st = this.mark(f, n);
\r
144 if (this.hasClass(n, s.url_cls) && !va.isAbsUrl(n))
\r
145 st = this.mark(f, n);
\r
147 if (this.hasClass(n, s.email_cls) && !va.isEmail(n))
\r
148 st = this.mark(f, n);
\r
150 if (this.hasClass(n, s.size_cls) && !va.isSize(n))
\r
151 st = this.mark(f, n);
\r
153 if (this.hasClass(n, s.id_cls) && !va.isId(n))
\r
154 st = this.mark(f, n);
\r
156 if (this.hasClass(n, s.min_cls, true)) {
\r
157 v = this.getNum(n, s.min_cls);
\r
159 if (isNaN(v) || parseInt(n.value) < parseInt(v))
\r
160 st = this.mark(f, n);
\r
163 if (this.hasClass(n, s.max_cls, true)) {
\r
164 v = this.getNum(n, s.max_cls);
\r
166 if (isNaN(v) || parseInt(n.value) > parseInt(v))
\r
167 st = this.mark(f, n);
\r
174 hasClass : function(n, c, d) {
\r
175 return new RegExp('\\b' + c + (d ? '[0-9]+' : '') + '\\b', 'g').test(n.className);
\r
178 getNum : function(n, c) {
\r
179 c = n.className.match(new RegExp('\\b' + c + '([0-9]+)\\b', 'g'))[0];
\r
180 c = c.replace(/[^0-9]/g, '');
\r
185 addClass : function(n, c, b) {
\r
186 var o = this.removeClass(n, c);
\r
187 n.className = b ? c + (o != '' ? (' ' + o) : '') : (o != '' ? (o + ' ') : '') + c;
\r
190 removeClass : function(n, c) {
\r
191 c = n.className.replace(new RegExp("(^|\\s+)" + c + "(\\s+|$)"), ' ');
\r
192 return n.className = c != ' ' ? c : '';
\r
195 tags : function(f, s) {
\r
196 return f.getElementsByTagName(s);
\r
199 mark : function(f, n) {
\r
200 var s = this.settings;
\r
202 this.addClass(n, s.invalid_cls);
\r
203 this.markLabels(f, n, s.invalid_cls);
\r
208 markLabels : function(f, n, ic) {
\r
211 nl = this.tags(f, "label");
\r
212 for (i=0; i<nl.length; i++) {
\r
213 if (nl[i].getAttribute("for") == n.id || nl[i].htmlFor == n.id)
\r
214 this.addClass(nl[i], ic);
\r