/** * expose messages. support i18n. */ function GLOBAL_MESSAGES_CONSTRUCTOR() { this['errors.default']='The fields highlighted below are incomplete or incorrect. Please correct these fields to complete the registration.'; } var GLOBAL_MESSAGES = new GLOBAL_MESSAGES_CONSTRUCTOR(); /** * Validator Functions */ /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /*$RCSfile: validateUtilities.js,v $ $Rev: 478676 $ $Date: 2008/02/05 07:01:03 $ */ /** * This is a place holder for common utilities used across the javascript validation * **/ /** * Retreive the name of the form * @param form The form validation is taking place on. */ function jcv_retrieveFormName(form) { // Please refer to Bugs 31534, 35127, 35294, 37315 & 38159 // for the history of the following code var formName; if (form.getAttributeNode) { if (form.getAttributeNode("id") && form.getAttributeNode("id").value) { formName = form.getAttributeNode("id").value; } else { formName = form.getAttributeNode("name").value; } } else if (form.getAttribute) { if (form.getAttribute("id")) { formName = form.getAttribute("id"); } else { formName = form.attributes["name"]; } } else { if (form.id) { formName = form.id; } else { formName = form.name; } } return formName; } /** * Handle error messages. * @param messages Array of error messages. * @param focusField Field to set focus on. */ function jcv_handleErrors(messages, fields, focusField) { pd_highlightFields(fields); pd_printErrors(messages); if (focusField && focusField != null) { var doFocus = true; if (focusField.disabled || focusField.type == 'hidden') { doFocus = false; } if (doFocus && focusField.style && focusField.style.visibility && focusField.style.visibility == 'hidden') { doFocus = false; } if (doFocus) { focusField.focus(); } } } function pd_highlightFields(fields) { pd_resetHighlights(); /** * If any error, hide "msgBoxGood" information */ if(fields.length > 0 && $('div.msgBoxGood').length > 0) $('div.msgBoxGood').hide(); for(var i=0;i0&&field[0].type == 'radio') { for(var i=0;i 0) $('#mainSearchMsg').html('').attr('class',''); if(messages[0]){ if(typeof formErrorsId == 'undefined' ) formErrorsId = "formErrors"; $("#"+formErrorsId).html(pd_showErrorMessage(messages[0])).attr('class','msgBoxError'); } } /** * print out each message */ function pd_showErrorMessage(message) { return "

"+getRealMessage(message)+"

"; } /** * substitute real message for placeholder as ${...} */ function getRealMessage(message) { if(typeof message != "string" ) return message; if(!GLOBAL_MESSAGES) { alert("No global messages instance has been initiated!"); return message; } var holders = message.match(/\$\{[\w\.]+\}/g); if(holders == null) return message; for(var i=0;i iMax) { if (i == 0) { focusField = field; } errorFields[i] = field; errorMessages[i++] = oMaxLength[x][1]; isValid = false; } } } if (errorMessages.length > 0) { jcv_handleErrors(errorMessages,errorFields, focusField); } return isValid; } /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Check to see if fields must contain a value. * Fields are not checked if they are disabled. * * @param form The form validation is taking place on. */ function validateRequired(form) { var isValid = true; var focusField = null; var i = 0; var errorFields = new Array(); var errorMessages = new Array(); var oRequired = eval('new ' + jcv_retrieveFormName(form) + '_required()'); for (var x in oRequired) { if (!jcv_verifyArrayElement(x, oRequired[x])) { continue; } var field = form[oRequired[x][0]]; if (!jcv_isFieldPresent(field)) { errorFields[i] = field; errorMessages[i++] = oRequired[x][1]; isValid=false; } else if ((field.type == 'hidden' || field.type == 'text' || field.type == 'textarea' || field.type == 'file' || field.type == 'radio' || field.type == 'checkbox' || field.type == 'select-one' || field.type == 'password')) { var value = ''; // get field's value if (field.type == "select-one") { var si = field.selectedIndex; if (si >= 0) { value = field.options[si].value; } } else if (field.type == 'radio' || field.type == 'checkbox') { if (field.checked) { value = field.value; } } else { value = field.value; } if (trim(value).length == 0) { if ((i == 0) && (field.type != 'hidden')) { focusField = field; } errorFields[i] = field; errorMessages[i++] = oRequired[x][1]; isValid = false; } } else if (field.type == "select-multiple") { var numOptions = field.options.length; lastSelected=-1; for(loop=numOptions-1;loop>=0;loop--) { if(field.options[loop].selected) { lastSelected = loop; value = field.options[loop].value; break; } } if(lastSelected < 0 || trim(value).length == 0) { if(i == 0) { focusField = field; } errorFields[i] = field; errorMessages[i++] = oRequired[x][1]; isValid=false; } } else if ((field.length > 0) && (field[0].type == 'radio' || field[0].type == 'checkbox')) { isChecked=-1; for (loop=0;loop < field.length;loop++) { if (field[loop].checked) { isChecked=loop; break; // only one needs to be checked } } if (isChecked < 0) { if (i == 0) { focusField = field[0]; } errorFields[i] = field; errorMessages[i++] = oRequired[x][1]; isValid=false; } } } if (errorMessages.length > 0) { jcv_handleErrors(errorMessages, errorFields,focusField); } return isValid; } // Trim whitespace from left and right sides of s. function trim(s) { return s.replace( /^\s*/, "" ).replace( /\s*$/, "" ); } /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /*$RCSfile: validateEmail.js,v $ $Rev: 478676 $ $Date: 2008/02/05 07:01:02 $ */ /** * Check to see if fields are a valid email address. * Fields are not checked if they are disabled. * @param form The form validation is taking place on. */ function validateEmail(form) { var bValid = true; var focusField = null; var i = 0; var errorFields = new Array(); var errorMessages = new Array(); var oEmail = eval('new ' + jcv_retrieveFormName(form) + '_email()'); for (var x in oEmail) { if (!jcv_verifyArrayElement(x, oEmail[x])) { continue; } var field = form[oEmail[x][0]]; if (!jcv_isFieldPresent(field)) { continue; } if ((field.type == 'hidden' || field.type == 'text' || field.type == 'textarea') && (field.value.length > 0)) { if (!jcv_checkEmail(field.value)) { if (i == 0) { focusField = field; } errorFields[i] = field; errorMessages[i++] = oEmail[x][1]; bValid = false; } } } if (errorMessages.length > 0) { jcv_handleErrors(errorMessages, errorFields, focusField); } return bValid; } var gEmailPattern = /^[a-zA-Z0-9]+([\\.\\-_][a-zA-Z0-9]+)*@[a-zA-Z0-9]+([\\.\\-][a-zA-Z0-9]+)*[\\.][a-zA-Z]{2,4}$/; /** * Email is optional */ function jcv_checkEmail(emailStr) { if(emailStr.length == 0) return true; if(emailStr.length > 97) return false; return gEmailPattern.test(emailStr); } /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /*$RCSfile: validateMask.js,v $ $Rev: 478676 $ $Date: 2008/02/05 07:01:03 $ */ /** * Check to see if fields are a valid using a regular expression. * Fields are not checked if they are disabled. * @param form The form validation is taking place on. */ function validateMask(form) { var isValid = true; var focusField = null; var i = 0; var errorFields = new Array(); var errorMessages = new Array(); var oMasked = eval('new ' + jcv_retrieveFormName(form) + '_mask()'); for (var x in oMasked) { if (!jcv_verifyArrayElement(x, oMasked[x])) { continue; } var field = form[oMasked[x][0]]; if (!jcv_isFieldPresent(field)) { continue; } if ((field.type == 'hidden' || field.type == 'text' || field.type == 'textarea' || field.type == 'password' || field.type == 'file') && (field.value.length > 0)) { if (!jcv_matchPattern(field.value, oMasked[x][2]("mask"))) { if (i == 0) { focusField = field; } errorFields[i] = field; errorMessages[i++] = oMasked[x][1]; isValid = false; } } } if (errorMessages.length > 0) { jcv_handleErrors(errorMessages,errorFields, focusField); } return isValid; } function jcv_matchPattern(value, mask) { return mask.exec(value); } /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /*$RCSfile: validateMinLength.js,v $ $Rev: 478676 $ $Date: 2008/02/05 07:01:03 $ */ /** * A field is considered valid if greater than the specified minimum. * Fields are not checked if they are disabled. * * Caution: Using validateMinLength() on a password field in a * login page gives unnecessary information away to hackers. While it only slightly * weakens security, we suggest using it only when modifying a password. * @param form The form validation is taking place on. */ function validateMinLength(form) { var isValid = true; var focusField = null; var i = 0; var errorFields = new Array(); var errorMessages = new Array(); var oMinLength = eval('new ' + jcv_retrieveFormName(form) + '_minlength()'); for (var x in oMinLength) { if (!jcv_verifyArrayElement(x, oMinLength[x])) { continue; } var field = form[oMinLength[x][0]]; if (!jcv_isFieldPresent(field)) { continue; } if ((field.type == 'hidden' || field.type == 'text' || field.type == 'password' || field.type == 'textarea')) { /* Adjust length for carriage returns - see Bug 37962 */ var lineEndLength = oMinLength[x][2]("lineEndLength"); var adjustAmount = 0; if (lineEndLength) { var rCount = 0; var nCount = 0; var crPos = 0; while (crPos < field.value.length) { var currChar = field.value.charAt(crPos); if (currChar == '\r') { rCount++; } if (currChar == '\n') { nCount++; } crPos++; } var endLength = parseInt(lineEndLength); adjustAmount = (nCount * endLength) - (rCount + nCount); } var iMin = parseInt(oMinLength[x][2]("minlength")); if ((trim(field.value).length > 0) && ((field.value.length + adjustAmount) < iMin)) { if (i == 0) { focusField = field; } errorFields[i] = field; errorMessages[i++] = oMinLength[x][1]; isValid = false; } } } if (errorMessages.length > 0) { jcv_handleErrors(errorMessages,errorFields, focusField); } return isValid; }