Friday, August 14, 2009
Simple Checkbox Validation in Javascript (Generic)
Kathiravan Manoharan
10:29:00 AM
checkbox
,
checkbox validation
,
javascript validation
No comments
:
The following is the function for simple checkbox validation:HTML PART
<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/>
<br/><br/>
<input type="submit" value="validate" onclick="return validateCheckBox( 'cbox[]' )">
</form>
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;
}
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment