An Entrepreneur, Coach, IT Consultant, Strategic Adviser, and a Traveler craving to explore and contribute to forming a better society.

Friday, August 14, 2009

Simple Checkbox Validation in Javascript (Generic)

No comments :
The following is the function for simple checkbox validation:


<form method=post action="xyz.html">
<input type="checkbox" name="cbox[]" value="one" />one<br/>
<input type="checkbox" name="cbox[]" value="two" />two<br/>
<input type="checkbox" name="cbox[]" value="three" />three<br/>
<input type="submit" value="validate" onclick="return validateCheckBox( 'cbox[]' )">

Validation PART

if( ! validateCheckBox( 'cbox[]' ) )
alert( "Please select any of the checkboxes" );

Validation FUNCTION

function validateCheckBox( checkBoxName ){
cboxArray = document.getElementsByName( checkBoxName );
//alert( cboxArray.length )
counterFlag = 0;
for( i=0; i < cboxArray.length; i++ ){
if( cboxArray[ i ].checked ) counterFlag = 1;
if( counterFlag ) return true;
else return false;

No comments :