function $jt(){ var elements = new Array(); for (var i = 0; i < arguments.length; i++) { var element = arguments[i]; if (typeof element == 'string') element = document.getElementById(element); if (arguments.length == 1) return element; elements.push(element); } return elements; } //////// Validation library V2 ////////////////////////////// var errored = false; // Global for validate function var exClassName = new Object(); // Global for Changed classnames var styleAdded = false; // Global for checking sytle status var captchaErrored= true; // Global for validate captcha var submitclik= false; // page break object var pageBreak = new pageBreak(); function validate(elem,type,option){ // Main function if (typeof(pageBreak.formName) == 'undefined'){ pageBreak.formName = elem; pageBreak.setInitialStatus(); } var option = (option)? option : ""; if(!styleAdded){ // Add style for Error warnings var style = document.createElement('style'); var sprop = '.error{ border:2px red solid; background:#FCFCFC; }'; sprop += '.Errortext{ color:#FF0000;font-family:"Trebuchet MS"; font-size:11px;padding: 0 0 0 5px; }'; sprop += '.DivErrortext{ border:1px solid #ccc;margin:4px; padding:5px; background:lightyellow; color:#FF0000;font-family:"Trebuchet MS"; font-size:11px; }'; style.setAttribute("type", "text/css"); if (style.styleSheet){ // for IE style.styleSheet.cssText = sprop; } else { var newStyle = document.createTextNode(sprop); style.appendChild(newStyle); } document.getElementsByTagName('head')[0].appendChild(style); styleAdded = true; //don't add again. } /////////// //Checking for mail validation var checkmail = function(email){ var splitted = email.match("^(.+)@(.+)$"); if(splitted == null) return false; if(splitted[1] != null ) { var regexp_user=/^\"?[\w-_\.]*\"?$/; if(splitted[1].match(regexp_user) == null) return false; } if(splitted[2] != null) { var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,15}$/; if(splitted[2].match(regexp_domain) == null) { var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/; if(splitted[2].match(regexp_ip) == null) return false; } return true; } return false; } var warned = false; ////////////// // Function to run onsubmit; var onSubmit = function(elem){ submitclik= false; errored = false; var form = document.forms[elem]; for(var node = 0;node < form.length; node++) if((""+form[node].onblur).match("validate")) form[node].onblur(); if(errored){ if(!warned){ var inputs = form.getElementsByTagName('input'); for(var x =0; x< inputs.length; x++){ if(inputs[x].type == 'submit'){ var errdiv = document.createElement('div'); errdiv.className = 'DivErrortext'; //errdiv.innerHTML = 'Bitte f├╝llen Sie alle Pflichtfelder im Formular aus.'; errdiv.innerHTML = 'Please fill all required fields.'; inputs[x].parentNode.appendChild(errdiv); } } warned = true; } pageBreak.goErrorPage(); submitclik= true; return false; }else { if(document.getElementById('captchacode')!=undefined){ if(captchaErrored){ submitclik= true; return false; }else{ //form.submit(); validate_HTML(form); } }else{ //form.submit(); validate_HTML(form); } //form.submit(); } } ////////////// // Function to set error messages var getMsg = function(type){ switch(type){ case "LessThan": return "Maximum length: " + option['LessThan'] + " Characters" case "GreaterThan": return "Minimum length: " + option['GreaterThan'] + " Characters" case "Alphabetic": return "Not allowed characters." case "RequiredAlphabetic": return "This is a required field." case "Numeric": return "Only numeric characters allowed." case "RequiredNumeric": return "This is a required field which only allows numeric characters." case "Email": return "Please fill a valid email address." case "RequiredEmail": return "This is a required field which only allows a valid email address." case "Required": return "This is a required field." case "confirm": return "The values must match" case "Regex": return "The values don't match" case "Captcha": return "Please fill the verification code." case "RequiredCaptcha": return "The varification code was not valid, please try again." default: return "Undefined error occured." } } ///////////// // Function to check is error printed? var checkForErrorDiv = function(elem){ for(var node = 0;node < elem.parentNode.childNodes.length; node++) if(elem.parentNode.childNodes[node].className == "Errortext") return true; return false; } ///////////// // Function to define errors and print messages var error = function(elem, type, message){ correct(elem); if(!checkForErrorDiv(elem)){ div = document.createElement("span"); //div.innerHTML = (message)? message : getMsg(type); //div.innerHTML = (message)? message : ''; var currmsg=''; var currmsg= encodeURIComponent((message)? message : getMsg(type)); //div.innerHTML = (message)? message : '!'; div.innerHTML = (message)? message : ''; div.className = "Errortext"; elem.parentNode.appendChild(div); //elem.parentNode.insertBefore(div, elem.nextSibling); $(elem.parentNode).parent('tr').children('td:first-child').addClass('td_error_style'); } if(elem.className != "error") exClassName[elem.name] = elem.className; if(elem.type != "checkbox" && elem.type != "radio"){ elem.className = "error"; } errored = true; return true; } ///////////// // Function to revert Errored fields var correct = function(elem){ var parent = elem.parentNode; for(x = 0; x < parent.childNodes.length; x++){ var node = parent.childNodes[x]; if(node){ if(node.className == "Errortext") { node.parentNode.removeChild(node); $(node.parentNode).parent('tr').children('td:first-child').removeClass('td_error_style'); } if(node.className == "error") { node.className = exClassName[node.name]; $(node.parentNode).parent('tr').children('td:first-child').removeClass('td_error_style'); } } } } /////////////// // Check for optional validations if(option['LessThan']){ if(elem.value.length >= option['LessThan']){ error(elem,"LessThan",option['message']); return false; }else correct(elem); } if(option['GreaterThan']){ if(elem.value.length <= option['GreaterThan']){ error(elem,"GreaterThan", option['message']); return false; }else correct(elem); } if(option.confirm){ if(elem.value.toLowerCase() != $jt(option.confirm).value.toLowerCase()){ error(elem, "confirm", option['message']); error($jt(option.confirm), "confirm", option['message']); return false; }else{ correct(elem); correct($jt(option.confirm)); } } ////////////// // Make all the validations switch(type){ case "Alphabetic": var charpos = elem.value.search(/[^a-zA-Z\s\-\_\']/); if(charpos >= 0) error(elem,type,option['message']); else correct(elem); break; case "RequiredAlphabetic": if(elem.value.length <= 0) error(elem,type,option['message']); else{ var charpos = elem.value.search(/[^a-zA-Z\s\-\_\']/); if(charpos >= 0) error(elem,type,option['message']); else correct(elem); } break; case "Numeric": var charpos = elem.value.search(/[^0-9\.\,\s\-\_]/); if(charpos >= 0) error(elem,type,option['message']); else correct(elem); break; case "RequiredNumeric": if(elem.value.length <= 0) { error(elem,type,option['message']); }else{ var charpos = elem.value.search(/[^0-9\.\,\s\-\_]/); if(charpos >= 0) error(elem,type,option['message']); else correct(elem); } break; case "Email": if(elem.value.length > 0){ if(!checkmail(elem.value)) error(elem,type,option['message']); else correct(elem); } break; case "RequiredEmail": if(elem.value.length <= 0){ error(elem,type,option['message']); }else{ if(!checkmail(elem.value)) error(elem,type,option['message']); else correct(elem); } break; case "Captcha": if(elem.value.length <= 0) { error(elem,type,option['message']); }else{ var captchavalue= elem.value; /*$.post("ajax.php", {getcode:captchavalue}, function(data){ if(data=='Correct'){ correct(elem); }else if(data='Error'){ error(elem,type,option['message']); } }); */ xmlHttp=GetXmlHttpObject(); if (xmlHttp==null){ alert ("Your browser does not support AJAX!"); return; } if(ajxpath == undefined){ var ajxpath='/php/dyntpl/'; } var url= ajxpath + "ajax.php?getcode="+ captchavalue; xmlHttp.open("GET",url,true); xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState == 4) { if( xmlHttp.responseText== 'Correct'){ correct(elem); captchaErrored = false; if(submitclik== true && errored==false){ //alert(submitclik); document.frm.submit(); } }else if(xmlHttp.responseText== 'Error'){ error(elem,'RequiredCaptcha',option['message']); captchaErrored= true; } document.getElementById('loderdiv').innerHTML=''; } else { //document.getElementById('loderdiv').innerHTML= ''; document.getElementById('loderdiv').innerHTML= 'loading..'; } } xmlHttp.send(null); } break; case "Required": if(elem.type == "checkbox" || elem.type == "radio"){ var parent = elem.parentNode; var ok = false; for(x = 0; x < parent.childNodes.length; x++) if(parent.childNodes[x].checked == true) ok = true; if(ok) correct(elem,true); else error(elem,type,option['message']); }else{ if(elem.options) // Here can be edited for checking text to "Please Select one" or similar text // in this example it looks for blank