bgneal@312: /** bgneal@312: * validate.js bgneal@312: * bgneal@312: * Copyright 2009, Moxiecode Systems AB bgneal@312: * Released under LGPL License. bgneal@312: * bgneal@312: * License: http://tinymce.moxiecode.com/license bgneal@312: * Contributing: http://tinymce.moxiecode.com/contributing bgneal@312: */ bgneal@312: bgneal@312: /** bgneal@312: // String validation: bgneal@312: bgneal@312: if (!Validator.isEmail('myemail')) bgneal@312: alert('Invalid email.'); bgneal@312: bgneal@312: // Form validation: bgneal@312: bgneal@312: var f = document.forms['myform']; bgneal@312: bgneal@312: if (!Validator.isEmail(f.myemail)) bgneal@312: alert('Invalid email.'); bgneal@312: */ bgneal@312: bgneal@312: var Validator = { bgneal@312: isEmail : function(s) { bgneal@312: return this.test(s, '^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$'); bgneal@312: }, bgneal@312: bgneal@312: isAbsUrl : function(s) { bgneal@312: return this.test(s, '^(news|telnet|nttp|file|http|ftp|https)://[-A-Za-z0-9\\.]+\\/?.*$'); bgneal@312: }, bgneal@312: bgneal@312: isSize : function(s) { bgneal@442: return this.test(s, '^[0-9.]+(%|in|cm|mm|em|ex|pt|pc|px)?$'); bgneal@312: }, bgneal@312: bgneal@312: isId : function(s) { bgneal@312: return this.test(s, '^[A-Za-z_]([A-Za-z0-9_])*$'); bgneal@312: }, bgneal@312: bgneal@312: isEmpty : function(s) { bgneal@312: var nl, i; bgneal@312: bgneal@312: if (s.nodeName == 'SELECT' && s.selectedIndex < 1) bgneal@312: return true; bgneal@312: bgneal@312: if (s.type == 'checkbox' && !s.checked) bgneal@312: return true; bgneal@312: bgneal@312: if (s.type == 'radio') { bgneal@312: for (i=0, nl = s.form.elements; i parseInt(v)) bgneal@312: st = this.mark(f, n); bgneal@312: } bgneal@312: } bgneal@312: bgneal@312: return st; bgneal@312: }, bgneal@312: bgneal@312: hasClass : function(n, c, d) { bgneal@312: return new RegExp('\\b' + c + (d ? '[0-9]+' : '') + '\\b', 'g').test(n.className); bgneal@312: }, bgneal@312: bgneal@312: getNum : function(n, c) { bgneal@312: c = n.className.match(new RegExp('\\b' + c + '([0-9]+)\\b', 'g'))[0]; bgneal@312: c = c.replace(/[^0-9]/g, ''); bgneal@312: bgneal@312: return c; bgneal@312: }, bgneal@312: bgneal@312: addClass : function(n, c, b) { bgneal@312: var o = this.removeClass(n, c); bgneal@312: n.className = b ? c + (o != '' ? (' ' + o) : '') : (o != '' ? (o + ' ') : '') + c; bgneal@312: }, bgneal@312: bgneal@312: removeClass : function(n, c) { bgneal@312: c = n.className.replace(new RegExp("(^|\\s+)" + c + "(\\s+|$)"), ' '); bgneal@312: return n.className = c != ' ' ? c : ''; bgneal@312: }, bgneal@312: bgneal@312: tags : function(f, s) { bgneal@312: return f.getElementsByTagName(s); bgneal@312: }, bgneal@312: bgneal@312: mark : function(f, n) { bgneal@312: var s = this.settings; bgneal@312: bgneal@312: this.addClass(n, s.invalid_cls); bgneal@442: n.setAttribute('aria-invalid', 'true'); bgneal@312: this.markLabels(f, n, s.invalid_cls); bgneal@312: bgneal@312: return false; bgneal@312: }, bgneal@312: bgneal@312: markLabels : function(f, n, ic) { bgneal@312: var nl, i; bgneal@312: bgneal@312: nl = this.tags(f, "label"); bgneal@312: for (i=0; i