Overview: This code sample provides an inline javascript
source code which can be used to determine a form element
validity: such as numeric or valid email. Additional parameters
can be passed, indicating whether the field is required and
the maximum length.
(Try it) Cut and Paste (sample)
Javascript Validation
Step 1: Including validation.js
Step 2: Source Code for validation.js
1. Returns the current date in mm/dd/yyyy format
function now()
{
var stDate;
var now;
now_date= new Date();
stDate=(now_date.getMonth()+1)+"/"+now_date.getDate()+"/"+now_date.getYear();
return(stDate);
}
1. Determines if a string has a valid email structure
2. Functionality from planet-source-code
function IsEmail(sFieldValue)
{
var checkEmail = "@.";
var checkStr = sFieldValue;
var EmailValid = false;
var EmailAt = false;
var EmailPeriod = false;
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkEmail.length; j++)
{
if (ch == checkEmail.charAt(j) && ch == "@")
EmailAt = true;
if (ch == checkEmail.charAt(j) && ch == ".")
EmailPeriod = true;
if (EmailAt && EmailPeriod)
break;
if (j == checkEmail.length)
break;
}
// if both the @ and . were in the string
if (EmailAt && EmailPeriod)
{
EmailValid = true
break;
}
}
return(EmailValid);
}
1. Determines if a string is completely numeric
2. Functionality from Planet-Source-Code
function IsNumeric(sFieldValue)
{
var checkOK = "0123456789";
var checkStr = sFieldValue;
var allValid = true;
var allNum = "";
if (checkStr=="")
{
return(false);
}
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
{
if (ch == checkOK.charAt(j))
break;
}
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch != ",")
{
allNum += ch;
}
if (!allValid)
{
allValid =false;
}
}
return(allValid);
}
1. Determine is a field value is numeric or an email
2. Checks if the field is required
3. Checks if the field length has exceeded the maximum size
function FieldValidation(sFieldName, sFieldValue, sRule, bRequired, maxLength)
{
var sErrorMessage;
sErrorMessage="";
switch(sRule)
{
case "Numeric":
if (IsNumeric(sFieldValue)==false)
{
sErrorMessage+=sFieldName + " is not numeric ";
}
break;
case "Email":
if (IsEmail(sFieldValue)==false)
{
sErrorMessage+=sFieldName + " is not an email ";
}
break;
}
/*------------------------------------------check if field is required---------------------------*/
if ( (bRequired==true) && (sFieldValue!=""))
{
if (sFieldValue.length==0)
{
sErrorMessage+=sFieldName + " is required ";
}
}
else if ( (bRequired==true) && (sFieldValue==""))
{
sErrorMessage+=sFieldName + " is required ";
}
/*------------------------------------check for maxlength--------------------------------------*/
if ((sFieldValue!="") && (maxLength>0))
{
if (sFieldValue.length>maxLength)
{
sErrorMessage+=sFieldName + " exceeded the maximum length of " + maxLength + " characters ";
}
}
return(sErrorMessage);
}
1. Displays the error message in a popup window
function displayErrorMessage(sErrorMsg)
{
win = window.open(",", 'popup', 'height = 200 width=200 toolbar = yes titlebar=yes status = no resizeable=yes scrollbars=yes');
win.document.write("Data Validation Errors ");
win.document.write("" + sErrorMsg + "");
}
Attaching javascript code (OnClick)
1. Define a sErrorMsg variable
2. Pass the fieldname, fieldvalue,type,required, and maxlength
parameters to the FieldValidation function
3. If sErrorMsg has errors display them in a popup window.