PHP Classes

File: forms.validate.js

Recommend this page to a friend!
  Classes of Oliver Leuyim Angel   Forms on the go   forms.validate.js   Download  
File: forms.validate.js
Role: Auxiliary data
Content type: text/plain
Description: The js alerts script for the class
Class: Forms on the go
Create forms to insert or update MySQL records
Author: By
Last change:
Date: 10 years ago
Size: 6,690 bytes
 

Contents

Class file image Download
String.prototype.trim=function(){ return this.replace(/^\s*|\s*$/g,''); } String.prototype.ltrim=function(){ return this.replace(/^\s*/g,''); } String.prototype.rtrim=function(){ return this.replace(/\s*$/g,''); } function FIC_checkForm(e) { var errs = new Array(); if (typeof(e) == "string") { e = xGetElementById(e); if (!e) { return true; } }var elm=e; if (!e.nodeName) { elm = (e.srcElement) ? e.srcElement : e.target; } if (elm.nodeName.toLowerCase() != 'form') { elm = searchUp(elm,'form'); } var all_valid = true;var f_in = elm.getElementsByTagName('input'); var f_sl = elm.getElementsByTagName('select'); var f_ta = elm.getElementsByTagName('textarea');for (i=0;i<f_in.length;i++) { if (f_in[i].type.toLowerCase() != 'submit' && f_in[i].type.toLowerCase() != 'button' && f_in[i].type.toLowerCase() != 'hidden') {if (isVisible(f_in[i])) { var cname = ' '+f_in[i].className.replace(/^\s*|\s*$/g,'')+' '; cname = cname.toLowerCase(); var inv = f_in[i].value.trim(); var t = f_in[i].type.toLowerCase(); var cext = '';if (t == 'text' || t == 'password') { var valid = FIC_checkField(cname,f_in[i]); } else if(t == 'radio' || t == 'checkbox'){ var valid = FIC_checkRadCbx(cname,f_in[i],f_in); cext = '-cr'; } else { var valid = true; } if (valid) { removeClassName(f_in[i],'validation-failed'+cext); addClassName(f_in[i],'validation-passed'+cext); } else { removeClassName(f_in[i],'validation-passed'+cext); addClassName(f_in[i],'validation-failed'+cext); if (f_in[i].getAttribute('title')){ errs[errs.length] = f_in[i].getAttribute('title'); }all_valid = false;}}}}for (i=0;i<f_ta.length;i++) { if (isVisible(f_ta[i])) { var cname = ' '+f_ta[i].className.replace(/^\s*|\s*$/g,'')+' '; cname = cname.toLowerCase(); var valid = FIC_checkField(cname,f_ta[i]); if (valid) { removeClassName(f_ta[i],'validation-failed'); addClassName(f_ta[i],'validation-passed'); } else { removeClassName(f_ta[i],'validation-passed'); addClassName(f_ta[i],'validation-failed'); if (f_ta[i].getAttribute('title')){ errs[errs.length] = f_ta[i].getAttribute('title'); } all_valid = false; } } }for (i=0;i<f_sl.length;i++) { if (isVisible(f_sl[i])) { var cname = ' '+f_sl[i].className.replace(/^\s*|\s*$/g,'')+' '; cname = cname.toLowerCase(); var valid = FIC_checkSel(cname,f_sl[i]); if (valid) { removeClassName(f_sl[i],'validation-failed-sel'); addClassName(f_sl[i],'validation-passed-sel'); } else { removeClassName(f_sl[i],'validation-passed-sel'); addClassName(f_sl[i],'validation-failed-sel'); if (f_sl[i].getAttribute('title')){ errs[errs.length] = f_sl[i].getAttribute('title'); } all_valid = false; } } } if (!all_valid) { if (errs.length > 0){ alert("Hemos encontrado los siguiente(s) error(es):\n\n * "+errs.join("\n * ")+"\n\nPor favor, compruebe los campos y vuelva a intentarlo"); } else { alert('Algunos valores requeridos no son correctas. Por favor, compruebe los elementos en rojo.'); } YAHOO.util.Event.stopEvent(e); } return all_valid; } function FIC_checkField(c,e) { var valid = true; var t = e.value.trim(); if (c.indexOf(' required ') != -1 && t.length == 0) { valid = false; } if (c.indexOf(' required ') != -1){ var m = e.getAttribute('minlength'); if (m && Math.abs(m) > 0){ if (e.value.length < Math.abs(m)){ valid = false; } } } if (c.indexOf(' validate-number ') != -1 && isNaN(t) && t.match(/[^\d]/)) { valid = false; } else if (c.indexOf(' validate-digits ') != -1 && t.replace(/ /,'').match(/[^\d]/)) { valid = false; } else if (c.indexOf(' validate-alpha ') != -1 && !t.match(/^[a-zA-Z]+$/)) { valid = false; } else if (c.indexOf(' validate-alphanum ') != -1 && t.match(/\W/)) { valid = false; } else if (c.indexOf(' validate-date ') != -1) { var d = new date(t); if (isNaN(d)) { valid = false; } } else if (c.indexOf(' validate-email ') != -1 && !t.match(/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/)) { valid = false; if (c.indexOf(' required ') == -1 && t.length == 0) { valid = true; } } else if (c.indexOf(' validate-url ') != -1 && !t.match(/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i)) { valid = false; } else if (c.indexOf(' validate-date-au ') != -1 && !t.match(/^(\d{2})\/(\d{2})\/(\d{4})$/)) { valid = false; } else if (c.indexOf(' validate-currency-dollar ') != -1 && !t.match(/^\$?\-?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}\d*(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$/)) { valid = false; } else if (c.indexOf(' validate-regex ') != -1) { var r = RegExp(e.getAttribute('regex')); if (r && ! t.match(r)) { valid = false; } } return valid;} function FIC_checkRadCbx(c,e,f){ var valid = true; if (c.indexOf(' validate-one-required ') != -1) { valid = false; for (var i=0;i<f.length;i++){ if(f[i].name.toLowerCase() == e.name.toLowerCase() && f[i].checked){ valid = true; break; } } } return valid;} function FIC_checkSel(c,e){ var valid = true; if (c.indexOf(' validate-not-first ') != -1 && e.selectedIndex == 0) { valid = false; } else if (c.indexOf(' validate-not-empty ') != -1 && e.options[e.selectedIndex].value.length == 0) { valid = false; } return valid;} function addClassName(e,t) { if (typeof e == "string") { e = xGetElementById(e); } var ec = ' ' + e.className.replace(/^\s*|\s*$/g,'') + ' '; var nc = ec; t = t.replace(/^\s*|\s*$/g,''); if (ec.indexOf(' '+t+' ') == -1) { nc = ec + t; } e.className = nc.replace(/^\s*|\s*$/g,''); return true;} function removeClassName(e,t) { if (typeof e == "string") { e = xGetElementById(e); } var ec = ' ' + e.className.replace(/^\s*|\s*$/g,'') + ' '; var nc = ec; t = t.replace(/^\s*|\s*$/g,''); if (ec.indexOf(' '+t+' ') != -1) { nc = ec.replace(' ' + t.replace(/^\s*|\s*$/g,'') + ' ',' '); } e.className = nc.replace(/^\s*|\s*$/g,''); return true;} function attachToForms(e) {var frms = document.getElementsByTagName('form');for(var i=0;i<frms.length;i++) {YAHOO.util.Event.addListener(frms[i], "submit", FIC_checkForm);}} function isVisible(e) { if (typeof e == "string") { e = xGetElementById(e); } while (e.nodeName.toLowerCase() != 'body' && e.style.display.toLowerCase() != 'none' && e.style.visibility.toLowerCase() != 'hidden') { e = e.parentNode; } if (e.nodeName.toLowerCase() == 'body') { return true; } else{return false;}} function searchUp(elm,findElm,debug) { if(typeof(elm) == 'string') { elm = xGetElementById(elm); } while (elm && elm.parentNode && elm.nodeName.toLowerCase() != findElm && elm.nodeName.toLowerCase() != 'body') { elm = elm.parentNode; } return elm;} function xGetElementById(e) { if(typeof(e)!='string') return e; if(document.getElementById) e=document.getElementById(e); else if(document.all) e=document.all[e]; else e=null; return e;} YAHOO.util.Event.addListener(window, "load", attachToForms);