This document shows some examples of validation using javascript. It talks about Date Validation, Moving Items between lists and ways to verify checking of checkboxes.

1.1 Date Validation:

This function checks the date format

function checkDate(date)

{

var ret = false;

var months = [‘JAN’, ‘FEB’, ‘MAR’, ‘APR’, ‘MAY’, ‘JUN’, ‘JUL’, ‘AUG’,
‘SEP’, ‘OCT’, ‘NOV’, ‘DEC’];

var i = 0;

if(date != null && date.length == 11 && date.charAt(2) == ‘-‘
&& date.charAt(6) == ‘-‘ && !isNaN(date.substr(0,2)) 
&& !isNaN(date.substr(7,4)))

{

month = date.substr(3, 3);

while(!ret && i < 12)

{

ret = (month.toUpperCase() == months[i++])

}

}

return ret;

}

1.2 Move Between List

The function moves selected item from list to another list

function moveBWList(fromList, toList)

{

/* alert(‘fromList selected index = ‘ + fromList.selectedIndex +

‘ text = ‘ + fromList.options[fromList.selectedIndex].text + 

‘ value = ‘ + fromList.options[fromList.selectedIndex].value +

‘ toList length = ‘ + toList.length);*/

var optionToBeMoved = null;

for(i=0; i < fromList.length; i++)

{

if( fromList.options[i].selected)

{

//alert(‘fromList.options[‘ + i + ‘] = ‘ + fromList.options[i].text);

optionToBeMoved = new Option(fromList.options[i].text,

fromList.options[i].value);

toList.options[toList.length] = optionToBeMoved;

fromList.options.remove(i–);

}

}

}

1.3 Is Items checked:

The function checks if items are checked in the list

function isChecked(checkList, num, mesg)

{

var ret = false;

var count=0;

for(i=0; i < checkList.length; i++)

{

if(checkList[i].checked)

{

count++;

if(count >= num)

{

ret = true;

break;

}

}

}

if(!ret)

{

alert(mesg);

}

return ret;

}