function trim(untrimmed) { return untrimmed.replace('/(^\s+)|(\s+$)/g', ''); } function submitScript(inForm) { var submitValid=true; var missingVars = ''; var incorrectVars = ''; var longVars = ''; for (var x=0;x < inForm.length;x++) { if (inForm[x].type=="text" || inForm[x].type=="password") { if (inForm[x].getAttribute("required")=='true') { var t = inForm[x].value; if (trim(t)=="") { missingVars += inForm[x].getAttribute("fullname") + "\n"; //alert(inForm[x].getAttribute("fullname") + ' is missing.'); submitValid=false; } } else if (inForm[x].getAttribute("required")=='login') { var t = (inForm[x].getAttribute("value")); re = new RegExp("^[-a-zA-Z0-9]+$","g"); if (!re.test(t)) { incorrectVars += inForm[x].getAttribute("fullname") + "\n"; //alert(inForm[x].getAttribute("fullname") + ' is incorrect.'); submitValid=false; } } else if (inForm[x].getAttribute("required")=='text') { var t = (inForm[x].getAttribute("value")); re = new RegExp("^[a-zA-Z0-9]+$","g"); if (!re.test(t)) { incorrectVars += inForm[x].getAttribute("fullname") + "\n"; //alert(inForm[x].getAttribute("fullname") + ' is incorrect.'); submitValid=false; } } else { //alert(inForm[x].getAttribute("fullname") + ' is not required.'); } if ( inForm[x].getAttribute("dataType")=="int" && trim(inForm[x].value)!="" && !isInteger(inForm[x].value)) { incorrectVars += inForm[x].getAttribute("fullname") + "\n"; //alert(inForm[x].getAttribute("fullname") + ' is not an integer.'); submitValid=false; } if ( inForm[x].getAttribute("dataType")=="real" && trim(inForm[x].value)!="" && isNaN(inForm[x].value)) { incorrectVars += inForm[x].getAttribute("fullname") + "\n"; //alert(inForm[x].getAttribute("fullname") + ' is not a number.'); submitValid=false; } if (inForm[x].getAttribute("dataType")=="currency" && trim(inForm[x].value)!="") { if (!currencyValid(trim(inForm[x].value))) { incorrectVars += inForm[x].getAttribute("fullname") + "\n"; //alert(inForm[x].getAttribute("fullname") + ' is not a valid amount of money.'); submitValid=false; } } if (inForm[x].getAttribute("dataType")=="email" && trim(inForm[x].value)!="") { if (!emailValid(trim(inForm[x].value))) { incorrectVars += inForm[x].getAttribute("fullname") + "\n"; //alert(inForm[x].getAttribute("fullname") + ' is not a valid e-mail address.'); submitValid=false; } } if (inForm[x].getAttribute("dataType")=="date" && trim(inForm[x].value)!="") { var tempDate = trim(inForm[x].value); var tempName = trim(inForm[x].name); var tdo = parseDate(tempDate); if (tempDate == "dd-mm-yyyy" || tempDate == "dd/mm/yyyy") { document.getElementById('field_hidden_'+tempName.substring( tempName.indexOf('_')+1)).setAttribute('value',''); } else if (tdo) { //alert('field_hidden_'+tempName.substring( tempName.indexOf('_')+1)); document.getElementById('field_hidden_'+tempName.substring( tempName.indexOf('_')+1)).setAttribute('value',tdo.getFullYear()+'-'+ (tdo.getMonth()+1)+'-'+tdo.getDate()); } else { incorrectVars += inForm[x].getAttribute("fullname") + "\n"; //alert(inForm[x].getAttribute("fullname") + ' is invalid.'); submitValid=false; } } } else if (inForm[x].type=="file") { if (inForm[x].getAttribute("required")=='true') { var t = inForm[x].value; if (trim(t)=="") { missingVars += inForm[x].getAttribute("fullname") + "\n"; //alert(inForm[x].getAttribute("fullname") + ' is missing.'); submitValid=false; } } } else if (inForm[x].type=='textarea') { if (inForm[x].getAttribute("required")=='true') { var t = inForm[x].value; if (trim(t)=="") { missingVars += inForm[x].getAttribute("fullname") + "\n"; //alert(inForm[x].getAttribute("fullname") + ' is missing.'); submitValid=false; } if (inForm[x].getAttribute("maxlength") != "" && inForm[x].getAttribute("maxlength") != null) { if (parseInt(inForm[x].getAttribute("maxlength"))<=inForm[x].value.length) { longVars += inForm[x].getAttribute("fullname") + "\n"; } } } } } if (!submitValid) { var totalMessage = ''; if (missingVars != '') { totalMessage += 'Required fields are missing:\n'+ missingVars + '\n\n'; } if (incorrectVars != '') { totalMessage += 'Various fields are incorrect:\n'+ incorrectVars + '\n\n'; } if (longVars != '') { totalMessage += 'Various fields have exceeded their maximum length:\n'+ longVars + '\n\n'; } if (totalMessage != '') { alert(totalMessage); } } else { inForm.submit(); } } function checkTextarea(event,ta) { if (ta.getAttribute("maxlength") != "" && ta.getAttribute("maxlength") != null) { if (parseInt(ta.getAttribute("maxlength"))<=ta.value.length) { ta.value = ta.value.substring(0, ta.getAttribute("maxlength")); } } else { return true; } } function checkPassword(p1,p2) { passwordValid = true; if (p1.value!=p2.value) { alert('Passwords are not equal.'); passwordValid=false; } else if (p1.value.length < 4) { alert('Password requires at least 4 characters.'); passwordValid=false; } return passwordValid; } function isDigit() { return ((event.keyCode >= 48) && (event.keyCode <= 57)); //return ((kc >= 48) && (kc <= 57)); } function charIsDigit (c){ return ((c >= "0") && (c <= "9")); } function emailValid(emailAddress) { var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/ ; return filter.test(emailAddress); } function currencyValid(currency) { //var filter1 = '/^\$?\d{1,3}(,?\d{3})*(\.\d{1,2})?$/' ; //var filter2 = '/^\$?\d{1.3}(.?\d{3})*(\,\d{1.2})?$/' ; //return filter1.test(currency) || filter2.test(currency); return true; } function isInteger(num) { var i; var isInt = true; for (i = 0; i < num.length; i++) { if (!charIsDigit(num.substring(i,i+1))) isInt = false; } return isInt; } //srmonth = (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );