Professional Documents
Culture Documents
Version
Modification Comments
Modified
By
Reviewed
By
Date
Draft 1.0
Sunil Soni
11-09-2011
Comments
Contents
1.
Introduction......................................................................................................................................... 3
It is often a good idea to put your common JavaScript validation functions into a common file, so you
can include the file from all pages which need the form validation. ........................................................... 3
2.
3.
FromDate toDate......................................................................................................................... 3
2.2.
IsDate 1:....................................................................................................................................... 4
2.3.
Aalternative 2: ............................................................................................................................. 5
2.4.
Page 1
3.2.
4.
getCheckedValue(): ................................................................................................................... 12
4.2.
setCheckedValue():.................................................................................................................... 13
4.3.
isCheckedRadio: ........................................................................................................................ 13
5.
6.
7.
8.
9.
10.
10.1. ....................................................................................................................................................... 17
11.
12.
12.1.
Validation 1: .......................................................................................................................... 19
12.2.
Validation 2: .......................................................................................................................... 19
13.
14.
Links: .............................................................................................................................................. 20
Page 2
1. Introduction
It is often a good idea to put your common JavaScript validation functions into a
common file, so you can include the file from all pages which need the form
validation
2. Date Validation
2.1.
FromDate toDate
This is the code for date comparison fromDate should be less than toDate
var objFromDate = document.getElementById("fromdate").value;
var objToDate = document.getElementById("todate").value;
var FromDate = new Date(objFromDate);
var ToDate = new Date(objToDate);
var valCurDate = new Date();
val CurDate = valCurDate.getMonth()+1 + "/" + valCurDate.getDate() + "/" + valCurDate.getYear();
var CurDate = new Date(valCurDate);
if(FromDate > ToDate)
{
alert(fromname + " should be less than " + toname);
return false;
}
else if(FromDate > CurDate)
{
alert("From date should be less than current date");
return false;
}
else if(ToDate > CurDate)
Page 3
{
alert("To date should be less than current date");
return false;
}
2.2.
IsDate 1:
function isDate(dtStr){
var daysInMonth = DaysArray(12)
var pos1=dtStr.indexOf(dtCh)
var pos2=dtStr.indexOf(dtCh,pos1+1)
var strMonth=dtStr.substring(0,pos1)
var strDay=dtStr.substring(pos1+1,pos2)
var strYear=dtStr.substring(pos2+1)
strYr=strYear
if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
if (strMonth.charAt(0)=="0" && strMonth.length>1)
strMonth=strMonth.substring(1)
for (var i = 1; i <= 3; i++) {
if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
}
month=parseInt(strMonth)
day=parseInt(strDay)
year=parseInt(strYr)
if (pos1==-1 || pos2==-1){
alert("The date format should be : mm/dd/yyyy")
return false
}
Page 4
2.3.
Aalternative 2:
function isDate(value)
{
var dateRegEx = new
RegExp(/^(?:(?:(?:0?[13578]|1[02])(\/|-)31)|(?:(?:0?[1,39]|1[0-2])(\/|-)(?:29|30)))(\/|-)(?:[1-9]\d\d\d|\d[19]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(?:(?:0?[1-9]|1[0-2])(\/|)(?:0?[1-9]|1\d|2[0-8]))(\/|-)(?:[1-9]\d\d\d|\d[19]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(0?2(\/|-)29)(\/|-
Page 5
)(?:(?:0[48]00|[13579][26]00|[2468][048]00)|(?:\d\d)?(?:0[48]
|[2468][048]|[13579][26]))$/);
if (dateRegEx.test(value))
{
return true;
}
return false;
}
2.4.
var pos1=enqiryFromDate.indexOf(dtCh)
Page 6
var pos2=enqiryFromDate.indexOf(dtCh,pos1+1)
var strDay=enqiryFromDate.substring(0,pos1)
var strMonth=enqiryFromDate.substring(pos1+1,pos2)
var strYear=enqiryFromDate.substring(pos2+1)
strYr=strYear
if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
for (var i = 1; i <= 3; i++) {
if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
}
month=parseInt(strMonth)
day=parseInt(strDay)
year=parseInt(strYr)
var enqiryFromDateObj = new Date(Date.parse(month + '/' + day + '/' + year));
Page 7
Page 8
return false;
}
else{
//alert('OK');
return true;
}
}
}
Page 9
3. Email Validaton:
Every email is made up for 5 parts:
1.
2.
3.
4.
5.
Valid Examples:
bobby.jo@filltank.net
jack+jill@hill.com
the-stand@steven.king.com
Invalid Examples:
3.1.
JavaScript Code1
function emailValidator(elem, helperMsg){
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z09]{2,4}$/;
if(elem.value.match(emailExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
Example:
<script type='text/javascript'>
function emailValidator(elem, helperMsg){
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
Page 10
return false;
}
}
</script>
<form>
Email: <input type='text' id='emailer'/>
<input type='button'
onclick="emailValidator1(document.getElementById('emailer'), 'Not a
Valid Email')"
value='Check Field' />
</form>
3.2.
JavaScript Code 2
function trim(s)
{
return s.replace(/^\s+|\s+$/, '');
}
function validateEmail(fld) {
var error="";
var tfld = trim(fld.value);
field with whitespace trimmed off
var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
// value of
if (fld.value == "") {
fld.style.background = 'Yellow';
error = "You didn't enter an email address.\n";
} else if (!emailFilter.test(tfld)) {
//test email
for illegal characters
fld.style.background = 'Yellow';
error = "Please enter a valid email address.\n";
} else if (fld.value.match(illegalChars)) {
fld.style.background = 'Yellow';
error = "The email address contains illegal characters.\n";
} else {
fld.style.background = 'White';
}
return error;
}
Page 11
4.1.
getCheckedValue():
return the value of the radio button that is checked return an empty string if none are checked, or
there are no radio buttons
function getCheckedValue(radioObj) {
if(!radioObj)
return "";
var radioLength = radioObj.length;
if(radioLength == undefined)
if(radioObj.checked)
return radioObj.value;
else
return "";
for(var i = 0; i < radioLength; i++) {
if(radioObj[i].checked) {
return radioObj[i].value;
}
}
return "";
}
Page 12
4.2.
setCheckedValue():
set the radio button with the given value as being checked , do nothing if there are no radio buttons
if the given value does not exist, all the radio buttons are reset to unchecked
function setCheckedValue(radioObj, newValue) {
if(!radioObj)
return;
var radioLength = radioObj.length;
if(radioLength == undefined) {
radioObj.checked = (radioObj.value == newValue.toString());
return;
}
for(var i = 0; i < radioLength; i++) {
radioObj[i].checked = false;
if(radioObj[i].value == newValue.toString()) {
radioObj[i].checked = true;
}
}
}
4.3.
isCheckedRadio:
function isCheckedRadio(){
var isSelected=false;
Page 13
if( document.getElementById('tranType1').checked){
isSelected=true;
}
if( document.getElementById('tranType2').checked){
isSelected=true;
}
return isSelected;
}
Page 14
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
</script>
<form>
Numbers Only: <input type='text' id='numbers'/>
<input type='button'
onclick="isNumeric(document.getElementById('numbers'), 'Numbers Only
Please')"
value='Check Field' />
</form>
Example:
<script type='text/javascript'>
function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
Page 15
}
}
</script>
<form>
Letters Only: <input type='text' id='letters'/>
<input type='button'
onclick="isAlphabet(document.getElementById('letters'), 'Letters Only
Please')"
value='Check Field' />
</form>
By combining both the isAlphabet and isNumeric functions into one we can check to see if a text input
contains only letters and numbers.
function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
Example:
<script type='text/javascript'>
Page 16
Page 17
}
var illegalChars = /\W/;
// allow only letters, numbers, and underscores
if (illegalChars.test(strng)) {
error = "The username contains illegal characters.\n";
}
10.2 UserId
Supports alphabets and numbers no special characters except underscore('_') min 3 and max 20
characters.
var ck_username = /^[A-Za-z0-9_]{3,20}$/;
Page 18
12.1.
Validation 1:
Then we count the length of the number. It should have exactly ten digits any more or
less, and we reject it.
if (!(stripped.length == 10)) {
error = "The phone number is the wrong length.
Make sure you included an area code.\n";
}
12.2.
Validation 2:
function validatePhone(fld) {
var error = "";
var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');
if (fld.value == "") {
error = "You didn't enter a phone number.\n";
fld.style.background = 'Yellow';
} else if (isNaN(parseInt(stripped))) {
error = "The phone number contains illegal characters.\n";
fld.style.background = 'Yellow';
} else if (!(stripped.length == 10)) {
error = "The phone number is the wrong length. Make sure you included an area code.\n";
fld.style.background = 'Yellow';
}
return error
Page 19
14. Links:
Page 20