function FieldChecker() { //Variables var msgMaxRows = "Het maximum aantal rijen voor het veld is ."; var msgMaxChars = "Het maximum aantal karakters voor het veld is ."; var msgRequired = "Het veld is een verplicht veld."; var msgIncorrectExtension = "Het uploaden van het bestand is niet toegestaan in het veld .\nDe volgende bestandtypen zijn wel toegstaan: ."; var msgIncorrectMailAddress = "Het veld bevat een ongeldig mailadres." var msgIncorrectPostalCode = "Het veld bevat een ongeldige postcode." var msgIncorrectPercentage = "Het veld moet een percentage bevatten tussen en ." var RequiredFieldColor = "#FFFFFF"; //Properties this.Fields = new Array(75); //Used for storing checkFields this.FileFields = new Array(); this.FieldCount = 0; //Current fieldCount // initialize the member function references // for the class prototype if (typeof(_FieldChecker_prototype_called) == 'undefined') { _FieldChecker_prototype_called = true; FieldChecker.prototype.addField = _addField; FieldChecker.prototype.addFileField = _addFileField; FieldChecker.prototype.checkFields = _checkFields; FieldChecker.prototype.setMsgMaxRows = _setMsgMaxRows; FieldChecker.prototype.setMsgMaxChars = _setMsgMaxChars; FieldChecker.prototype.setMsgRequired = _setMsgRequired; FieldChecker.prototype.setExtensions = _setExtensions; FieldChecker.prototype.setMsgIncorrectExtension = _setMsgIncorrectExtension; FieldChecker.prototype.clearAllFields = _clearAllFields; FieldChecker.prototype.getFieldByName = _getFieldByName; FieldChecker.prototype.setMsgIncorrectMailAddress = _setMsgIncorrectMailAddress; FieldChecker.prototype.setMsgIncorrectPostalCode = _setMsgIncorrectPostalCode; FieldChecker.prototype.setMsgIncorrectPercentage = _setMsgIncorrectPercentage; FieldChecker.prototype.setRequiredFieldColor = _setRequiredFieldColor; } function _setMsgIncorrectMailAddress(Msg) { msgIncorrectMailAddress = Msg; } function _getFieldByName(Name) { for (var i = 1; i <= this.FieldCount; i++) { if (this.Fields[i].Fieldname == Name) { return this.Fields[i]; } } } function _addField(Fieldname, FriendlyName, maxRows, maxChars, Required) { if (document.getElementsByName(Fieldname).length > 0) { this.FieldCount++; this.Fields[this.FieldCount] = new checkField(Fieldname, FriendlyName, maxRows, maxChars, Required); if (Required == true) document.getElementsByName(Fieldname)[0].style.backgroundColor = RequiredFieldColor; } else { //alert("The field " + Fieldname + " does not exist..."); } } function _addFileField(Fieldname, FriendlyName, Extensions, Required) { if (document.getElementsByName(Fieldname).length > 0) { this.FileFields[this.FileFields.length] = new checkFileField(Fieldname, FriendlyName, Extensions, Required); if (Required == true) document.getElementsByName(Fieldname)[0].style.backgroundColor = RequiredFieldColor; } else { //alert("The field " + Fieldname + " does not exist..."); } } function _clearAllFields() { for (var i = 1; i <= this.FieldCount; i++) { document.getElementsByName(this.Fields[i].Fieldname)[0].value = ""; } } function _checkFields() { //Loop through Fields in FieldChecker for (var i = 1; i <= this.FieldCount; i++) { //Check Maximum Number of Rows if (this.Fields[i].maxRows > 0) { if (getRowCount(this.Fields[i].Fieldname) > this.Fields[i].maxRows) { var ErrorMsg = replaceSubstring(msgMaxRows, "", this.Fields[i].FriendlyName); ErrorMsg = replaceSubstring(ErrorMsg, "", "" + this.Fields[i].maxRows); alert(ErrorMsg); document.getElementsByName(this.Fields[i].Fieldname)[0].focus(); return false; } } //Check Maximum Number of Characters if (this.Fields[i].maxChars > 0) { if (document.getElementsByName(this.Fields[i].Fieldname)[0].value.length > this.Fields[i].maxChars) { var ErrorMsg = replaceSubstring(msgMaxChars, "", this.Fields[i].FriendlyName); ErrorMsg = replaceSubstring(ErrorMsg, "", "" + this.Fields[i].maxChars); alert(ErrorMsg); document.getElementsByName(this.Fields[i].Fieldname)[0].focus(); return false; } } //Check Field is Required/Empty if (this.Fields[i].Required == true) { if (document.getElementsByName(this.Fields[i].Fieldname)[0].value == "") { var ErrorMsg = replaceSubstring(msgRequired, "", this.Fields[i].FriendlyName); alert(ErrorMsg); document.getElementsByName(this.Fields[i].Fieldname)[0].focus(); return false; } } //Check Field contains file with Correct Extension if (document.getElementsByName(this.Fields[i].Fieldname)[0].type == "file") { var CorrectExtension = false; var strExtensions = "" var Extension = document.getElementsByName(this.Fields[i].Fieldname)[0].value; if (Extension != "") { Extension = Extension.substring(Extension.length-4,Extension.length); for (var j=0;j", this.Fields[i].FriendlyName); ErrorMsg = replaceSubstring(ErrorMsg, "", strExtensions.substring(0,strExtensions.length-1)); ErrorMsg = replaceSubstring(ErrorMsg, "", document.getElementsByName(this.Fields[i].Fieldname)[0].value); alert(ErrorMsg); document.getElementsByName(this.Fields[i].Fieldname)[0].focus(); return false; } } } //Check correct MailAddress if (this.Fields[i].isMail == true) { var EMailVal = document.getElementsByName(this.Fields[i].Fieldname)[0].value; var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; if (!filter.test(EMailVal)) { alert(replaceSubstring(msgIncorrectMailAddress, "", this.Fields[i].FriendlyName)) document.getElementsByName(this.Fields[i].Fieldname)[0].focus(); return false; } } //Check correct PostalCode if (this.Fields[i].isPostalCode == true) { var newString=""; var iError = false; for (var j=0;j= "a") & (currChar <= "z")) | ((currChar >= "0") & (currChar <= "9"))) newString += currChar.toLowerCase(); } var re = /^\d{4}[a-z]{2}$/ //Check for 4 digits folowed by 2 chars if (newString.search(re)==-1) { alert(replaceSubstring(msgIncorrectPostalCode, "", this.Fields[i].FriendlyName)) document.getElementsByName(this.Fields[i].Fieldname)[0].focus(); return false; } else { document.getElementsByName(this.Fields[i].Fieldname)[0].value = newString.toUpperCase(); } } //Check correct Percentage if (this.Fields[i].isPercentage == true) { var Percentage = document.getElementsByName(this.Fields[i].Fieldname)[0].value; var re = /^\d{1,9999}$/ //Check for 4 digits only var iError = false; if (Percentage.search(re)!=-1) { if ((Percentage < this.Fields[i].minPercentage) | (Percentage > this.Fields[i].maxPercentage)) iError = true } else iError = true; if (iError == true) { var ErrMessage = ""; ErrMessage = replaceSubstring(msgIncorrectPercentage, "", this.Fields[i].FriendlyName); ErrMessage = replaceSubstring(ErrMessage,"","" + this.Fields[i].minPercentage); ErrMessage = replaceSubstring(ErrMessage,"","" + this.Fields[i].maxPercentage); alert(ErrMessage); document.getElementsByName(this.Fields[i].Fieldname)[0].focus(); return false; } } //Check correct number if (this.Fields[i].isNumeric == true) { var getal = document.getElementsByName(this.Fields[i].Fieldname)[0].value; var re = /^\d{*}[,]{0,1}\d{*}$/ //Check for 4 digits folowed by 2 chars } } // check file fields for (var i=0; i < this.FileFields.length; i++) { // controleer of veld verplicht is if (document.getElementsByName(this.FileFields[i].Fieldname)[0].value == "" && this.FileFields[i].Required) { var ErrorMsg = replaceSubstring(msgRequired, "", this.FileFields[i].FriendlyName); alert(ErrorMsg); document.getElementsByName(this.FileFields[i].Fieldname)[0].focus(); return false; } if(document.getElementsByName(this.FileFields[i].Fieldname)[0].value != "") { // controleer extensie var FilePath = document.getElementsByName(this.FileFields[i].Fieldname)[0].value; var FileExt = FilePath.substring(FilePath.lastIndexOf(".")+1, FilePath.length); var isValidExtension = false; for (var ix=0; ix < this.FileFields[i].Extensions.length; ix++) { if(FileExt.toLowerCase() == replaceSubstring(this.FileFields[i].Extensions[ix].toLowerCase(), " ","")) isValidExtension = true; } if(!isValidExtension) { var ErrorMsg = replaceSubstring(msgIncorrectExtension, "", this.FileFields[i].FriendlyName); ErrorMsg = replaceSubstring(ErrorMsg, "", this.FileFields[i].ExtensionsTXT); ErrorMsg = replaceSubstring(ErrorMsg, "", document.getElementsByName(this.FileFields[i].Fieldname)[0].value); alert(ErrorMsg); document.getElementsByName(this.FileFields[i].Fieldname)[0].focus(); return false; } } } //Specific checks from JS Header of HTML document in which FieldChecker is used try { return checkFieldsEx(); } catch(e){} return true; //All fields oké } function getRowCount(field) { var text = document.getElementsByName(field)[0].value; var split = text.split("\n"); return split.length; } function _setMsgMaxRows(Msg) { msgMaxRows = Msg; } function _setMsgMaxChars(Msg) { msgMaxChars = Msg; } function _setMsgRequired(Msg) { msgRequired = Msg; } function _setExtensions(passExt) { Extensions = passExt; } function _setMsgIncorrectExtension(Msg) { msgIncorrectExtension = Msg; } function _setMsgIncorrectPostalCode(Msg) { msgIncorrectPostalCode = Msg; } function _setMsgIncorrectPercentage(Msg) { msgIncorrectPercentage = Msg; } function _setRequiredFieldColor(Color) { RequiredFieldColor = Color; for (var i = 1; i <= this.FieldCount; i++) { if (this.Fields[i].Required == true) document.getElementsByName(this.Fields[i].Fieldname)[0].style.backgroundColor = RequiredFieldColor; } } function replaceSubstring(inputString, fromString, toString) { // Goes through the inputString and replaces every occurrence of fromString with toString var temp = inputString; if (fromString == "") { return inputString; } if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation) while (temp.indexOf(fromString) != -1) { var toTheLeft = temp.substring(0, temp.indexOf(fromString)); var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length); temp = toTheLeft + toString + toTheRight; } } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop var midStrings = new Array("~", "`", "_", "^", "#"); var midStringLen = 1; var midString = ""; // Find a string that doesn't exist in the inputString to be used // as an "inbetween" string while (midString == "") { for (var i=0; i < midStrings.length; i++) { var tempMidString = ""; for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; } if (fromString.indexOf(tempMidString) == -1) { midString = tempMidString; i = midStrings.length + 1; } } } // Keep on going until we build an "inbetween" string that doesn't exist // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string while (temp.indexOf(fromString) != -1) { var toTheLeft = temp.substring(0, temp.indexOf(fromString)); var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length); temp = toTheLeft + midString + toTheRight; } // Next, replace the "inbetween" string with the "toString" while (temp.indexOf(midString) != -1) { var toTheLeft = temp.substring(0, temp.indexOf(midString)); var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length); temp = toTheLeft + toString + toTheRight; } } // Ends the check to see if the string being replaced is part of the replacement string or not return temp; // Send the updated string back to the user } // Ends the "replaceSubstring" function } function checkField(Fieldname, FriendlyName, maxRows, maxChars, Required) { this.Fieldname = Fieldname; this.FriendlyName = FriendlyName; this.maxRows = maxRows; this.maxChars = maxChars; this.Required = Required; this.isMail = false; this.isPostalCode = false; this.isPercentage = false; this.minPercentage = 0; this.maxPercentage = 100; this.isNumeric = false; } function checkFileField(Fieldname, FriendlyName, Extensions, Required) { this.Fieldname = Fieldname; this.FriendlyName = FriendlyName; this.ExtensionsTXT = Extensions; this.Extensions = Extensions.split(","); this.Required = Required; }