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