// JavaScript Document /** * Based largely on Will Jessup's jQuery form validator * http://www.willjessup.com/?p=9 * removed most of the formatting issues from the front end so * it validates (no empty divs etc) * */ var val = new function() { $.fn.validate = { init: function(o) { switch(o.name){ case 'title': this.lengthcheck(o,2,150); break; case 'url_title': this.lengthcheck(o,2,150); break; case 'staff_id': this.staff_id(o); break; case 'username': this.username(o); break; case 'name': this.lengthcheck(o,2,150); break; case 'firstname': this.namepart(o); break; case 'first_name': this.namepart(o); break; case 'new_first_name': this.namepart(o); break; case 'lastname': this.namepart(o); break; case 'last_name': this.namepart(o); break; case 'new_last_name': this.namepart(o); break; case 'permalink': this.permalink(o); break; case 'login': this.username(o); break; case 'password': this.password(o); break; case 'pass': this.password(o); break; case 're_pass': this.re_password(o); break; case 'url': this.url(o); break; case 'contact_email': this.email(o); break; case 'new_email': this.email(o); break; case 'email': this.email(o); break; case 'dob': this.dob(o); break; case 'contact_telephone': this.telephone(o); break; case 'phone': this.telephone(o); break; case 'contact_mobile': this.telephone(o); break; default: doValidate(o); } }, lengthcheck: function(o,min_len,max_len) { if (o.value.length < min_len || o.value.length > max_len ){ doError(o,o.id + ' (' + o.value.length + ') must be longer than ' + min_len + ' characters and less than ' + max_len + ' characters.'); } else { doValidate(o); } }, username: function(o) { var user = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/; if (!o.value.match(user)) { if (o.value.length < 6){ doError(o,o.id + ' (' + o.value.length + ') must be longer than 6 characters'); } else { doValidate(o); } } else { doError(o,'found{'+o.value.match(user)+'}, only a-z,A-Z,0-9,_,- characters allowed'); }; }, namepart: function(o) { var user = /[0-9(\*\(\)\[\]\+\_\.\,\/\?\!\[\]\{\}\|\+\*\"\:\;\"\~\\#\$\%\^\&\<\>)+]/; if (!o.value.match(user)) { this.lengthcheck(o,1,35);//doValidate(o); } else { doError(o,'found{'+o.value.match(user)+'}, only a-z,A-Z,\',`,- and space characters allowed'); }; }, password: function(o) { var pass = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\> )+]/; if (!o.value.match(pass)) { /*if (o.value.length < 6){ doError(o,o.id + ' (' + o.value.length + ') must be longer than 6 characters'); } else {*/ this.lengthcheck(o,6,100);//doValidate(o); /*}*/ } else { doError(o,'found{'+o.value.match(pass)+'}, only a-z,A-Z,0-9,_,- characters allowed'); }; }, re_password: function(o) { var pass = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\> )+]/; if (!o.value.match(pass)) { temp = o.id.split("_"); if ($("#"+temp[1]).val() == o.value){ this.lengthcheck(o,6,100);//doValidate(o); }else{ doError(o,' passwords do not match'); } } else { doError(o,'only alpha-numeric,_ and - characters allowed'); }; }, url: function(o) { var email = /^(http:\/\/|https:\/\/)+(([a-zA-Z0-9\_\-])+\.)+([a-zA-Z0-9]{2,4})+$/; if (o.value.match(email)) { this.lengthcheck(o,8,150); } else { if (o.value.length == 0){ doValidate(o); }else{ doError(o,'not a valid url: http(s)://'); } }; }, email: function(o) { var email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; if (o.value.match(email)) { doSuccess(o); } else { doError(o,'not a valid email'); }; }, telephone: function(o) { var invalid = /[A-Za-z(\*\(\)\[\]\_\.\,\/\?\!\[\]\{\}\|\*\"\:\;\"\~\\#\$\%\^\&\<\>)]/; if (!o.value.match(invalid)) { if (o.value.length != 10 ){ doError(o,'please include area code like: 0891235678'); }else{ doSuccess(o); } } else { doError(o,'found{'+o.value.match(invalid)+'}, not a valid telephone number.'); }; }, staff_id: function(o) { var staff_id = /^([0-9]{6})([A-Z]{1})+$/; if (o.value.match(staff_id)) { this.lengthcheck(o,6,7); } else { doError(o,'found{'+o.value.match(staff_id)+'}, not a valid Staff id'); }; }, permalink: function(o) { var valid_values = /[A-Z(\*\(\)\[\]\+\.\,\/\?\!\[\]\{\}\|\+\*\'\"\:\;\'\"\`\~\\#\$\%\^\&\<\> )+]/; if (o.value.match(valid_values) && o.value != 0) { doError(o,'found{'+o.value.match(valid_values)+'}, only a-z,0-9,_,- characters allowed.'); } else { this.lengthcheck(o,0,70);//doValidate(o);//doSuccess(o); }; }, release_code: function(o) { var valid_values = /^([A-Z]{1})([0-9]{3})\/([0-9]{2})$/; //var valid_values = /[a-z(\*\(\)\[\]\+\.\,\?\!\[\]\{\}\_\-\|\+\*\'\"\:\;\'\"\`\~\\#\$\%\^\&\<\> )+]/; if (!o.value.match(valid_values)) { doError(o,'must be of the form: C123/56'); } else { if (o.value.length != 7){ doError(o,o.id + ' (' + o.value.length + ') must be 7 characters'); } else { doValidate(o);//doSuccess(o); } }; }, dob: function(o) { var dob = /(0[1-9]|1[012])+\/(0[1-9]|[12][0-9]|3[01])+\/(19|20)\d\d/; if (o.value.match(dob)) { doSuccess(o); } else { doError(o,'not a valid date'); }; } }; function doSuccess(o) { $('#' + o.id).removeClass("error"); //$('#' + o.id).removeClass("validate"); $('#' + o.id).addClass("validated"); $('#' + o.id).parent().find(".warning").remove(); $('#' + o.id).parent().find(".loading").remove(); } function doError(o,m) { //$('#' + o.id).removeClass("validate"); $('#' + o.id).removeClass("validated"); $('#' + o.id).addClass("error"); $('#' + o.id).parent().find(".warning").remove(); $('#' + o.id).parent().append(''+m+''); $('#' + o.id).parent().find(".loading").remove(); } //private helper, validates each type after check function doValidate(o) { /* Any validation where server-side support is needed * this is business model functionality rather than * light, client side functionality */ $.post('/resources/helpers/validate.php', { id: o.id, value: o.value }, function(json) { eval("var args = " + json); if (args.success == true){ /* test for duplication class in form element * refer to duplicate checking serverside page */ if (o.className.match("no_duplicate")){ /* validate.cfm simply queries the database for a string match. * this obviously needs to be a custom written component because I don't know * what you're wanting to compare. The thinking would be that your id name * is the same as it is in the datasource, so you could simply do something like: * "SELECT id FROM datasource WHERE #id# = '#value#'" (coldfusion) * or * "SELECT id FROM datasource WHERE {$id} = '{$value}'" (php) */ $('#' + o.id).parent().prepend(''); $(".form").each(function(){ $.post('/resources/helpers/'+this.id+'/validate.php', { id: o.id, no_duplicate: true, value: o.value }, function(json) { eval("var args = " + json); if (args.success == true || duplicate_originals[o.id] == o.value){ doSuccess(args); } else { doError(args,args.msg); } });/**/ }); } else { doSuccess(args); } } else { doError(args,args.msg); } }); }; };