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