File: /home/awaldron/www/archive/blueliner/include/js/unobtrusive-validation/dtd/validate.js
//Created By: Chris Campbell
//www.particletree.com
window.onload = attachFormHandlers;
function attachFormHandlers()
{
// Make sure we're on a newer browser
if (document.getElementsByTagName)
{
var objInput = document.getElementsByTagName('input'); // get all input tags
var form = document.getElementById('form1') // get the form
for (var iCounter=0; iCounter<objInput.length; iCounter++)
{
objInput[iCounter].onchange = function(){return attach(this);} // attach the onchange event to each input tag
}
form.onsubmit = function(){return validate();} // atttach the onsubmit to the form
}
}
function attach(objInput)
{
sVal = objInput.value; //sVal is the value of the input field being validated
var sFeedBack; //feedback message sent back to the user
var sFeedbackLoc = objInput.getAttribute('message');
if (objInput.getAttribute('required') == "true")
{
sFeedback = validateRequired(sVal); //validateRequired() checks if it is invalid and sends back feedback
}
else sFeedback = "";
if (sVal != "") //if the value is blank we don't need to validate. If it is required, the word
//"required" will already be the feedback message from the validateRequired() function
{
// check the different validation cases (ie: email, phone, etc.)
switch (objInput.getAttribute('validate'))
{
case "date":
sFeedback = validateDate(sVal);
break;
case "email":
sFeedback = validateEmail(sVal);
break;
case "phone":
sFeedback = validatePhone(sVal);
break;
case "zip":
sFeedback = validateZip(sVal);
break;
case "password":
sFeedback = validatePassword(sVal);
break;
case "name":
sFeedback = validateName(sVal);
break;
case "numeric":
sFeedback = validateNumeric(sVal);
break;
}
}
// after validation is complete return the feedback
document.getElementById(sFeedbackLoc).innerHTML = sFeedback;
}
function validateRequired(sVal)
{
if (sVal != "") //if it is rquired and blank then it is an error and continues to be required
{
return ("Thank You");
}
else
{
gContinue = false;
return("Required");
}
}
function validateDate(sVal)
{
// our date regular expression (http://www.regexlib.com)
var regex=/(((0[13578]|10|12)([-.\/])(0[1-9]|[12][0-9]|3[01])([-.\/])(\d{4}))|((0[469]|11)([-.\/])([0][1-9]|[12][0-9]|30)([-.\/])(\d{4}))|((2)([-.\/])(0[1-9]|1[0-9]|2[0-8])([-.\/])(\d{4}))|((2)(\.|-|\/)(29)([-.\/])([02468][048]00))|((2)([-.\/])(29)([-.\/])([13579][26]00))|((2)([-.\/])(29)([-.\/])([0-9][0-9][0][48]))|((2)([-.\/])(29)([-.\/])([0-9][0-9][2468][048]))|((2)([-.\/])(29)([-.\/])([0-9][0-9][13579][26])))/;
// do the comparison, if we have a match write thank you or else the date is invalid
if (regex.test(sVal))
{
return "Thank You";
}
else
{
return "Invalid Date";
}
}
function validateEmail(sVal)
{
// our email regular expression (http://www.regexlib.com)
var regex=/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
// do the comparison, if we have a match write thank you or else the email is invalid
if (regex.test(sVal))
{
return "Thank You";
}
else
{
return "Invalid Email Address";
}
}
function validatePhone(sVal)
{
// our phone regular expression
// This expression is a very simplex expression that allows null values or 3 digits, dash,
//3 digits, dash, 4 digits. It validates a basic US phone number. Written by Jason N. Gaylord.(http://www.regexlib.com)
// Matches: [555-555-1212], [123-456-7890]
var regex=/^(\d{3}-\d{3}-\d{4})*$/;
// do the comparison, if we have a match write thank you or else the email is invalid
if (regex.test(sVal))
{
return "Thank You";
}
else
{
return "Invalid Phone";
}
}
function validateZip(sVal)
{
// our email regular expression
//Javascript matches US zipcodes not allowing all zeros in first 5 or +4 (http://www.regexlib.com)
// Matches: [12345], [12345-6789], [123456789]
var regex=/(^(?!0{5})(\d{5})(?!-?0{4})(-?\d{4})?$)/;
// do the comparison, if we have a match write thank you or else the email is invalid
if (regex.test(sVal))
{
return "Thank You";
}
else
{
return "Invalid ZipCode";
}
}
function validatePassword(sVal)
{
//Description: The password's first character must be a letter, it must contain at least 4 characters
//and no more than 15 characters and no characters other than letters, numbers and the underscore may be used
//Matches: [abcd], [aBc45DSD_sdf], [password] (http://www.regexlib.com)
var regex=/^[a-zA-Z]\w{3,14}$/;
// do the comparison, if we have a match write thank you or else the email is invalid
if (regex.test(sVal))
{
return "Thank You";
}
else
{
return "Invalid Password";
}
}
function validateName(sVal)
{
//This is the simplest RegEx for validating someone's name. The name can contain only alphabets(in either case) &
//should be of minimum length 4 & maximum length 32. Only white spaces are allowed apart from alphabets.
//Matches: [some body], [hey there], [hello] (http://www.regexlib.com)
var regex=/^([a-zA-z\s]{4,32})$/;
// do the comparison, if we have a match write thank you or else the email is invalid
if (regex.test(sVal))
{
return "Thank You";
}
else
{
return "Invalid Name";
}
}
function validateNumeric(sVal)
{
//Input for Numeric values. Handles negatives, and comma formatted values. Also handles a single decimal point
//Matches: [5,000], [-5,000], [100.044] (http://www.regexlib.com)
var regex=/^(\d|-)?(\d|,)*\.?\d*$/;
// do the comparison, if we have a match write thank you or else the email is invalid
if (regex.test(sVal))
{
return "Thank You";
}
else
{
return "Invalid Number";
}
}