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
101 nl[i].setAttribute('aria-invalid', false);
\r
104 c += this.validateElms(f, 'input');
\r
105 c += this.validateElms(f, 'select');
\r
106 c += this.validateElms(f, 'textarea');
\r
111 invalidate : function(n) {
\r
112 this.mark(n.form, n);
\r
115 getErrorMessages : function(f) {
\r
116 var nl, i, s = this.settings, field, msg, values, messages = [], ed = tinyMCEPopup.editor;
\r
117 nl = this.tags(f, "label");
\r
118 for (i=0; i<nl.length; i++) {
\r
119 if (this.hasClass(nl[i], s.invalid_cls)) {
\r
120 field = document.getElementById(nl[i].getAttribute("for"));
\r
121 values = { field: nl[i].textContent };
\r
122 if (this.hasClass(field, s.min_cls, true)) {
\r
123 message = ed.getLang('invalid_data_min');
\r
124 values.min = this.getNum(field, s.min_cls);
\r
125 } else if (this.hasClass(field, s.number_cls)) {
\r
126 message = ed.getLang('invalid_data_number');
\r
127 } else if (this.hasClass(field, s.size_cls)) {
\r
128 message = ed.getLang('invalid_data_size');
\r
130 message = ed.getLang('invalid_data');
\r
133 message = message.replace(/{\#([^}]+)\}/g, function(a, b) {
\r
134 return values[b] || '{#' + b + '}';
\r
136 messages.push(message);
\r
142 reset : function(e) {
\r
143 var t = ['label', 'input', 'select', 'textarea'];
\r
144 var i, j, nl, s = this.settings;
\r
149 for (i=0; i<t.length; i++) {
\r
150 nl = this.tags(e.form ? e.form : e, t[i]);
\r
151 for (j=0; j<nl.length; j++) {
\r
152 this.removeClass(nl[j], s.invalid_cls);
\r
153 nl[j].setAttribute('aria-invalid', false);
\r
158 validateElms : function(f, e) {
\r
159 var nl, i, n, s = this.settings, st = true, va = Validator, v;
\r
161 nl = this.tags(f, e);
\r
162 for (i=0; i<nl.length; i++) {
\r
165 this.removeClass(n, s.invalid_cls);
\r
167 if (this.hasClass(n, s.required_cls) && va.isEmpty(n))
\r
168 st = this.mark(f, n);
\r
170 if (this.hasClass(n, s.number_cls) && !va.isNumber(n))
\r
171 st = this.mark(f, n);
\r
173 if (this.hasClass(n, s.int_cls) && !va.isNumber(n, true))
\r
174 st = this.mark(f, n);
\r
176 if (this.hasClass(n, s.url_cls) && !va.isAbsUrl(n))
\r
177 st = this.mark(f, n);
\r
179 if (this.hasClass(n, s.email_cls) && !va.isEmail(n))
\r
180 st = this.mark(f, n);
\r
182 if (this.hasClass(n, s.size_cls) && !va.isSize(n))
\r
183 st = this.mark(f, n);
\r
185 if (this.hasClass(n, s.id_cls) && !va.isId(n))
\r
186 st = this.mark(f, n);
\r
188 if (this.hasClass(n, s.min_cls, true)) {
\r
189 v = this.getNum(n, s.min_cls);
\r
191 if (isNaN(v) || parseInt(n.value) < parseInt(v))
\r
192 st = this.mark(f, n);
\r
195 if (this.hasClass(n, s.max_cls, true)) {
\r
196 v = this.getNum(n, s.max_cls);
\r
198 if (isNaN(v) || parseInt(n.value) > parseInt(v))
\r
199 st = this.mark(f, n);
\r
206 hasClass : function(n, c, d) {
\r
207 return new RegExp('\\b' + c + (d ? '[0-9]+' : '') + '\\b', 'g').test(n.className);
\r
210 getNum : function(n, c) {
\r
211 c = n.className.match(new RegExp('\\b' + c + '([0-9]+)\\b', 'g'))[0];
\r
212 c = c.replace(/[^0-9]/g, '');
\r
217 addClass : function(n, c, b) {
\r
218 var o = this.removeClass(n, c);
\r
219 n.className = b ? c + (o != '' ? (' ' + o) : '') : (o != '' ? (o + ' ') : '') + c;
\r
222 removeClass : function(n, c) {
\r
223 c = n.className.replace(new RegExp("(^|\\s+)" + c + "(\\s+|$)"), ' ');
\r
224 return n.className = c != ' ' ? c : '';
\r
227 tags : function(f, s) {
\r
228 return f.getElementsByTagName(s);
\r
231 mark : function(f, n) {
\r
232 var s = this.settings;
\r
234 this.addClass(n, s.invalid_cls);
\r
235 n.setAttribute('aria-invalid', 'true');
\r
236 this.markLabels(f, n, s.invalid_cls);
\r
241 markLabels : function(f, n, ic) {
\r
244 nl = this.tags(f, "label");
\r
245 for (i=0; i<nl.length; i++) {
\r
246 if (nl[i].getAttribute("for") == n.id || nl[i].htmlFor == n.id)
\r
247 this.addClass(nl[i], ic);
\r