//====== //====== // // Template- VALIDATE_JS.IHT by B.Flynn 04-SEP-03 / 4-JUN-08 // // Purpose- // // General use validation functions. // // Notes- // // This file is included in JAVASCRIPT.IHT as the standard // javascript that is added to all web pages. // // Mandatory text field handling: // // Currently only INPUT TYPE="TEXT" fields can be marked as // mandatory. // // To use the processing in this file, the associated form // for the field must contain the following markup: // //
// // Use a DIV tag as follows to specify where to output the // associated error if the field is left blank: // //
// // - The div tag is case sensitive. The elements before the // "_DIV" must be the same case as the original input field // name, and the "_DIV" must be uppercase. // - The error does not indicate the field, so it should // normally be displayed immediately below the field. // // Additional supported markup: // - Suffixing the field with __MN will ensure it is a number: // //
// - The suffix [] is used by Rails to indicate a field is // a member of an array, and appears after any "__" flag // suffixes, e.g. // // // // ** Please update schedule3w.com /javascript/validate.js ** // ** with any changes. ** // // Other validation support: // // dy_restrict() // // Example- // // Mandatory form fields: // // //
//
// // //
//
// - Above requires a validation check of mandatory field // for "FirstName", requires a validation check of mandatory // field and number field for 'SchoolNumber'. // // Modifications- // // 15-SEP-03 BF / CH / PH 4=13331 // - Created // // 16-SEP-03 PH / PH / PH 4= // - add end script tag // // 10-DEC-03 JY / SH / KF 4=14582 // - add validate flag process. // // 5-MAR-04 BF / PH / PH 406= // - do not allow nullstring field values for required fields // // 27-SEP-04 SH / JY / AL 217=16015 // - Add function ValidateFormWSubmit. // // 27-OCT-04 TH / SH / TH 4= // - Improve documentation. // // 27-SEP-05 JY / SH / KB 12=14968 // - ValidateForm supports checkboxes. // // 19-OCT-05 JY / SH / JY 12= // - Add function ShowInLine to display a tag as inline. // // 2-AUG-06 TH / BH / TH 12=18594 // - Add dy_restrict. // // 22-SEP-06 TH / BH / TH 41=18667 // - Add dy_validateInteger. // - Improve docs for dy_restrict. // - Add dy_total. // // 28-SEP-06 TH / BH / TH 41=18667 // - In dy_validateInteger and dy_restrict, don't change // the element field unless necessary. // // 3-OCT-06 TH / BH / TH 41=18667 // - dy_total should normally be used in onkeyup rather // than onchange, adjust documentation to indicate this. // // 20-FEB-07 TH / BH / TH 17= // - Add further information on how to set up a mandatory field. // // 28-JUN-07 BG / TH / HD 73=18968 // - Support optional [] after flags, which is the array member // suffix used by Rails. // - Make a missing checkbox message, distinct from the missing // field message. // // 02-JUN-08 KB / BG / BG 254 // - change fillin -> fill in (two words) // //====== //====== // Mandatory form field validation javascript var required_field_identifier="_M"; var missing_field_message='* Please fill in above field.'; var missing_checkbox_message='* Please check at least one of the checkboxes above.'; var number_field_message='* Field value must be a number.'; var div_tag_suffix='_DIV'; var mandatoryFlag = /M/i; var numberFlag = /N/i; function ValidateFormWSubmit(theForm, verbose) { // - If form is submitted via a standard 'Submit' buttton, we use // this function instead of ValidateForm to prevent from a // 're-submit' warning when ValidateForm returns false. if(!ValidateForm(theForm, verbose)) { submitClicked=0; return false; } else { return true; } } function ValidateForm(theForm, verbose) { // // Inputs: // theForm: The HTML Form to validate // verbose: The method to display the required field constant // Values: // 1 = InnerHTML Of DIV tag // default = MessageBox // var foundError = false; var foundFieldError; for (i=0;i< theForm.elements.length;i++) { if ( theForm.elements[i].type=="text" || theForm.elements[i].type=="checkbox") { foundFieldError = false; // // Remove any white space that may be included in the text field name // var textFieldName = TrimString(theForm.elements[i].name); // // Extract the last 2 characters from the name of the text field. // var theSuffix=textFieldName.substring(textFieldName.length-2,textFieldName.length) // // Perform a string Comparsion to determine if this field is required (case insensitive) // if (theSuffix.toUpperCase()==required_field_identifier.toUpperCase()){ if (theForm.elements[i].type=="text"){ foundFieldError = CheckMandatoryField(theForm,verbose,i) || foundFieldError; }else { foundFieldError = CheckMandatoryCheckBoxes(theForm,theForm.elements[i].name, verbose) || foundFieldError; } } // validate flag is a '__' suffix var regex = /__(.*)(\[\])?$/; var reArr= regex.exec(textFieldName); if (reArr) { // found flag if(reArr[1].search(mandatoryFlag) != -1 && !foundFieldError) { if (theForm.elements[i].type=="text"){ foundFieldError = CheckMandatoryField(theForm,verbose,i) || foundFieldError; }else { foundFieldError = CheckMandatoryCheckBoxes(theForm,theForm.elements[i].name, verbose) || foundFieldError; } } if(reArr[1].search(numberFlag) != -1 && !foundFieldError) { foundFieldError = CheckNumberField(theForm,verbose,i) || foundFieldError; } } foundError = foundFieldError || foundError; } } if (foundError){ return false; } else { return true; } } function CheckMandatoryField(theForm,verboseMode,index){ // This field requires a value var foundError = false; var divID = GetDivID(theForm.elements[index].name); if (TrimString(theForm.elements[index].value)==""){ // // We have an empty required field. // ShowMessage(verboseMode, divID,missing_field_message); // // Set the focus to the offending text field // theForm.elements[index].focus(); foundError=true; } else { Hide(divID); } return foundError; } function CheckNumberField(theForm,verboseMode,index){ // This field requires a number value var foundError = false; var divID = GetDivID(theForm.elements[index].name); if(theForm.elements[index].value.length>0 && isNaN(theForm.elements[index].value)){ // found value is not a number ShowMessage(verboseMode,divID,number_field_message); foundError = true; } else { Hide(divID); } return foundError; } // The DivID is _M // e.g. fieldname = USERNAME__N // DivID = USERNAME_M_DIV // fieldname = USERNAME_M // DivID = USERNAME_M_DIV function GetDivID(elementName){ var divID = elementName; var tagInx = divID.lastIndexOf("__"); if (tagInx != -1) { divID = divID.substr(0,tagInx); divID += "_M"; } divID += div_tag_suffix; return divID; } function ShowMessage(VerboseMode, DivId, errorMessage) { // // Display a message to the user indicating that a required field is // missing a value. // // The function could be expanded to take the message to display to // the user as a parameter, but for now we'll use the constant above. // // // Inputs: // VerboseMode: The method to display the required field constant // Values: // 1 = InnerHTML Of DIV tag // default = MessageBox // DivId: The ID of the Div Tag that should display the message. // // Determine the display method of the Required Field Message! // switch (VerboseMode) { case 1: { // // Set the inner HTML of the DIV tag passed, if the DIV tag // does not exist or another error is raised we catch the exception // and display the required field message in the message box format! // try { document.getElementById(DivId).innerHTML = errorMessage; Show(DivId); } catch (exception) { alert(exception.message) alert(errorMessage); } break; } default: { // // Show the message as a message box // alert(errorMessage); break; } } } function Hide(divId){ // // Hides corresponding div specified by divId // document.getElementById(divId).style.display.visibility = 'hidden'; document.getElementById(divId).style.display='none'; } function Show(divId){ // // Shows corresponding div specified by divId // document.getElementById(divId).style.display.visibility = 'show'; document.getElementById(divId).style.display='block'; } function ShowInLine(divId){ // // Shows corresponding div specified by divId // document.getElementById(divId).style.display.visibility ='show'; document.getElementById(divId).style.display='inline'; } function TrimString(theString) { // // Remove any leading or trailing whitespace from a string! // while (theString.substring(0,1) == ' ') { theString = theString.substring(1,theString.length); } while (theString.substring(theString.length-1,theString.length) == ' ') { theString = theString.substring(0,theString.length-1); } return theString; } function CheckMandatoryCheckBoxes(theForm,fieldName,verboseMode){ var findError = true; var divID = GetDivID(fieldName); findError=!($(theForm).getInputs('checkbox',fieldName).pluck('checked').any()) if (findError){ // // We have an empty required field. // ShowMessage(verboseMode, divID,missing_checkbox_message); } else { Hide(divID); } return findError; } /* Purpose: - Restrict an element's value field to a given set of characters. Returns: - element.value - set to the left substring of element.value that has only valid characters. Notes: - Works on all versions of javascript. Example: */ var dy_digits = "0123456789"; function dy_restrict(element,valid_chars){ var result=""; for (var cursor=0; cursor */ function dy_validateInteger(element) { var result=""; var first_char=element.value.charAt(0); if (first_char == "-" || dy_digits.indexOf(first_char) != -1) { result+=first_char; for (var cursor=1; cursor