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