function formatNumber(inputNumber, numberPattern) { var numberString; var numberFormatted = ''; var numberIndex = 0; var formatIndex = 0; //If the input is a form field, use the field's value if (typeof(inputNumber)=='object') { numberString = "" + inputNumber.value; //If the input is a number or string, use the input value } else { numberString = "" + inputNumber; } //First check to see whether the input already matches the pattern //(which usually means that the user typed in the formatting //characters manually) numberIndex = 0; while (numberIndex < numberString.length) { //Using the pattern the user defined, insert digits in place of the # signs //and keep any other characters in the pattern if (numberPattern.charAt(numberIndex) != '#') { numberFormatted += numberPattern.charAt(numberIndex++); } else { numberFormatted += numberString.charAt(numberIndex++); } } //If the input does not match the pattern, we need to format it. if (numberFormatted != numberString) { numberFormatted = ''; //We only allow digits in the number. var illegalChars = /[^0-9]/; //Strip out non-digit characters so that we are left with the raw number var numberParts = numberString.split(illegalChars); numberString = numberParts.join(''); numberIndex = 0; formatIndex = 0; //Loop through the digits in the number and format them using the //pattern we've defined. By using the number's length instead of the //pattern's length, we can use this function to format a number //as the user types it into a text box. while (numberIndex < numberString.length) { //Exit when the formatted number matches the pattern the user defined if (numberFormatted.length == numberPattern.length) break; //Using the pattern the user defined, insert digits in place of the # signs //and keep any other characters in the pattern while (numberPattern.charAt(formatIndex) != '#') { numberFormatted += numberPattern.charAt(formatIndex++); } formatIndex++; numberFormatted += numberString.charAt(numberIndex++); } } //If the input was a form field, update that field's value if (typeof(inputNumber)=='object') { inputNumber.value = numberFormatted; } else { return numberFormatted; } } function validateForm(form) { var hasInvalidFields = false; var validField; var missingFieldText, invalidFieldText, invalidText = ""; var validColor, invalidColor; for (i = 0; i < form.elements.length; i++) { var field = form.elements[i] // Skip form buttons and elements that don't have one of the validation attributes set if ((field.getAttribute('validate') == 'true' || field.getAttribute('required') == 'true') && field.type != 'submit' && field.type != 'reset' && field.type != 'button') { missingFieldText = ""; invalidFieldText = ""; validColor = (field.getAttribute('validColor') ? field.getAttribute('validColor') : 'white'); invalidColor = (field.getAttribute('invalidColor') ? field.getAttribute('invalidColor') : 'yellow'); // Start each loop with the assumption that the field is valid validField = true; if (field.getAttribute('required') == 'true') { // The field may be required depending on the value of another // field or fields. Check the dependency specified in requiredIf. // If requiredIf evaluates true and the field has no value, the // field is invalid. if (field.getAttribute('requiredIf')) { if (eval(field.getAttribute('requiredIf'))) { validField = !(!field.value); } } else { // If the field is required and has no value, (and doesn't // depend on the value of another field or fields) the field // is invalid validField = !(!field.value); } if (!validField) missingFieldText = (field.getAttribute('missingText') ? field.getAttribute('missingText') : (field.getAttribute('displayName') ? field.getAttribute('displayName') : field.name) + " is missing.") } // If the field was not marked invalid above, validate the field contents. // (If it was already marked invalid, there's no need to run the // validation function.) if (validField) { validField = validateField(field); if (!validField) invalidFieldText = (field.getAttribute('invalidText') ? field.getAttribute('invalidText') : (field.getAttribute('displayName') ? field.getAttribute('displayName') : field.name) + " is invalid.") } //Generic formatting for valid (or missing) fields if (!validField) { invalidText += " - " + (missingFieldText ? missingFieldText : invalidFieldText) + "\n" if (!hasInvalidFields) try{field.focus()} catch(e){}; hasInvalidFields = true; field.style.backgroundColor = invalidColor; } else { field.style.backgroundColor = validColor; } } } if (hasInvalidFields) window.alert ('The form could not be submitted. One or more fields has missing or invalid data.\nPlease check the following fields and try your submission again.\n\n'+invalidText); return !hasInvalidFields } function validateField(field) { var validField = true; // look for the custom attribute "validate" if (field.getAttribute('validate') == 'true') { // We can't validate fields that have no value if (field.value) { // Validate the field based on the validationType attribute switch (field.getAttribute('validationType')) { case 'PhoneNumber': { // Remember, by its very name SCSO operates in Sacramento, California, USA. // Please do not bother me about international phone number formats. var phoneNumberRegExp = /^\(\d\d\d\) \d\d\d-\d\d\d\d$/; validField = phoneNumberRegExp.test(field.value); break; } case 'ZIPCode': { // Allow both basic ZIP and ZIP+4 formats var zipCodeBasicRegExp = /^\d\d\d\d\d$/; var zipPlus4RegExp = /^\d\d\d\d\d-\d\d\d\d$/; validField = zipCodeBasicRegExp.test(field.value) | zipPlus4RegExp.test(field.value); break; } case 'Email': { // Check for standard e-mail address format as // name@domain.com, but don't get any more fancy var emailRegExp = /.+\@.+\..+/; validField = emailRegExp.test(field.value); break; } case 'Date': { validField = VB_IsDate(field.value); if (validField) { field.value = VB_FormatDate(field.value); } break; } case 'Number': { validField = !isNaN(field.value); break; } case 'Alphanumeric': { var alphaNumericRegExp = /\W/; validField = !alphaNumericRegExp.test(field.value); break; } case 'AlphanumericPlus': { var alphaNumericRegExp = /[\W\@\.]/; validField = !alphaNumericRegExp.test(field.value); break; } case 'Custom': { validField = eval(field.getAttribute('validationData')); break; } default: { validField = false; break; } } } } // If the field validation is the result of the field // losing focus, we will highlight the field if it is // invalid, or remove highlighting if it is valid. if (window.event) { var validColor = (field.getAttribute('validColor') ? field.getAttribute('validColor') : 'white'); var invalidColor = (field.getAttribute('invalidColor') ? field.getAttribute('invalidColor') : 'yellow'); if (window.event.srcElement === field) { if (!validField) { field.style.backgroundColor = invalidColor; } else { field.style.backgroundColor = validColor; } } } return validField; }