4 * Copyright 2009, Moxiecode Systems AB
\r
5 * Released under LGPL License.
\r
7 * License: http://tinymce.moxiecode.com/license
\r
8 * Contributing: http://tinymce.moxiecode.com/contributing
\r
12 // String validation:
\r
14 if (!Validator.isEmail('myemail'))
\r
15 alert('Invalid email.');
\r
19 var f = document.forms['myform'];
\r
21 if (!Validator.isEmail(f.myemail))
\r
22 alert('Invalid email.');
\r
26 isEmail : function(s) {
\r
27 return this.test(s, '^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$');
\r
30 isAbsUrl : function(s) {
\r
31 return this.test(s, '^(news|telnet|nttp|file|http|ftp|https)://[-A-Za-z0-9\\.]+\\/?.*$');
\r
34 isSize : function(s) {
\r
35 return this.test(s, '^[0-9]+(%|in|cm|mm|em|ex|pt|pc|px)?$');
\r
38 isId : function(s) {
\r
39 return this.test(s, '^[A-Za-z_]([A-Za-z0-9_])*$');
\r
42 isEmpty : function(s) {
\r
45 if (s.nodeName == 'SELECT' && s.selectedIndex < 1)
\r
48 if (s.type == 'checkbox' && !s.checked)
\r
51 if (s.type == 'radio') {
\r
52 for (i=0, nl = s.form.elements; i<nl.length; i++) {
\r
53 if (nl[i].type == "radio" && nl[i].name == s.name && nl[i].checked)
\r
60 return new RegExp('^\\s*$').test(s.nodeType == 1 ? s.value : s);
\r
63 isNumber : function(s, d) {
\r
64 return !isNaN(s.nodeType == 1 ? s.value : s) && (!d || !this.test(s, '^-?[0-9]*\\.[0-9]*$'));
\r
67 test : function(s, p) {
\r
68 s = s.nodeType == 1 ? s.value : s;
\r
70 return s == '' || new RegExp(p).test(s);
\r
74 var AutoValidator = {
\r
79 number_cls : 'number',
\r
80 email_cls : 'email',
\r
82 required_cls : 'required',
\r
83 invalid_cls : 'invalid',
\r
88 init : function(s) {
\r
92 this.settings[n] = s[n];
\r
95 validate : function(f) {
\r
96 var i, nl, s = this.settings, c = 0;
\r
98 nl = this.tags(f, 'label');
\r
99 for (i=0; i<nl.length; i++)
\r
100 this.removeClass(nl[i], s.invalid_cls);
\r
102 c += this.validateElms(f, 'input');
\r
103 c += this.validateElms(f, 'select');
\r
104 c += this.validateElms(f, 'textarea');
\r
109 invalidate : function(n) {
\r
110 this.mark(n.form, n);
\r
113 reset : function(e) {
\r
114 var t = ['label', 'input', 'select', 'textarea'];
\r
115 var i, j, nl, s = this.settings;
\r
120 for (i=0; i<t.length; i++) {
\r
121 nl = this.tags(e.form ? e.form : e, t[i]);
\r
122 for (j=0; j<nl.length; j++)
\r
123 this.removeClass(nl[j], s.invalid_cls);
\r
127 validateElms : function(f, e) {
\r
128 var nl, i, n, s = this.settings, st = true, va = Validator, v;
\r
130 nl = this.tags(f, e);
\r
131 for (i=0; i<nl.length; i++) {
\r
134 this.removeClass(n, s.invalid_cls);
\r
136 if (this.hasClass(n, s.required_cls) && va.isEmpty(n))
\r
137 st = this.mark(f, n);
\r
139 if (this.hasClass(n, s.number_cls) && !va.isNumber(n))
\r
140 st = this.mark(f, n);
\r
142 if (this.hasClass(n, s.int_cls) && !va.isNumber(n, true))
\r
143 st = this.mark(f, n);
\r
145 if (this.hasClass(n, s.url_cls) && !va.isAbsUrl(n))
\r
146 st = this.mark(f, n);
\r
148 if (this.hasClass(n, s.email_cls) && !va.isEmail(n))
\r
149 st = this.mark(f, n);
\r
151 if (this.hasClass(n, s.size_cls) && !va.isSize(n))
\r
152 st = this.mark(f, n);
\r
154 if (this.hasClass(n, s.id_cls) && !va.isId(n))
\r
155 st = this.mark(f, n);
\r
157 if (this.hasClass(n, s.min_cls, true)) {
\r
158 v = this.getNum(n, s.min_cls);
\r
160 if (isNaN(v) || parseInt(n.value) < parseInt(v))
\r
161 st = this.mark(f, n);
\r
164 if (this.hasClass(n, s.max_cls, true)) {
\r
165 v = this.getNum(n, s.max_cls);
\r
167 if (isNaN(v) || parseInt(n.value) > parseInt(v))
\r
168 st = this.mark(f, n);
\r
175 hasClass : function(n, c, d) {
\r
176 return new RegExp('\\b' + c + (d ? '[0-9]+' : '') + '\\b', 'g').test(n.className);
\r
179 getNum : function(n, c) {
\r
180 c = n.className.match(new RegExp('\\b' + c + '([0-9]+)\\b', 'g'))[0];
\r
181 c = c.replace(/[^0-9]/g, '');
\r
186 addClass : function(n, c, b) {
\r
187 var o = this.removeClass(n, c);
\r
188 n.className = b ? c + (o != '' ? (' ' + o) : '') : (o != '' ? (o + ' ') : '') + c;
\r
191 removeClass : function(n, c) {
\r
192 c = n.className.replace(new RegExp("(^|\\s+)" + c + "(\\s+|$)"), ' ');
\r
193 return n.className = c != ' ' ? c : '';
\r
196 tags : function(f, s) {
\r
197 return f.getElementsByTagName(s);
\r
200 mark : function(f, n) {
\r
201 var s = this.settings;
\r
203 this.addClass(n, s.invalid_cls);
\r
204 this.markLabels(f, n, s.invalid_cls);
\r
209 markLabels : function(f, n, ic) {
\r
212 nl = this.tags(f, "label");
\r
213 for (i=0; i<nl.length; i++) {
\r
214 if (nl[i].getAttribute("for") == n.id || nl[i].htmlFor == n.id)
\r
215 this.addClass(nl[i], ic);
\r