CheckBox Validation Using javaScript
|
|
Checkbox validation:
Example
<html>
<head>
<script LANGUAGE="JavaScript">
function ValidateForm(form){
ErrorText= "";
if ( ( form.gender[0].checked == false ) && ( form.gender[1].checked == false ) )
{
alert ( "Please choose your Gender: Male or Female" );
return false;
}
if (ErrorText= "") { form.submit() }
}
</script>
</head>
<body>
<form name="feedback" action="#" method=post>
Your Gender: <input type="radio" name="gender" value="Male"> Male
<input type="checkbox" name="gender" value="Female"> Female
<input type="checkbox" name="SubmitButton" value="Submit" onClick="ValidateForm(this.form)">
<input type="reset" value="Reset">
</form>
</body>
</html>
|
|
|
The Output is Shown Here:
Your Gender:
Male
Female
|
Checkbox Validation
Example
<html>
<head>
<script type="text/javascript">
function checkButton(){
if(document.form1.button1.checked == true){
alert("Box1 is checked");
} else if(document.form1.button2.checked == true){
alert("Box 2 is checked");
}
}
</script>
</head>
<body>
<form name="form1">
<input type="chechbox" name=button1>Box 1
<br>
<input type="chechbox" name=button2 CHECKED>Box 2
<br>
<INPUT type="button" value="Get Checked" onClick='checkButton()'>
</form>
</body>
</html>
|
|
|
The Output is Shown Here:
|
Checkbox Validation
Example
<html>
<head>
<script type="text/javascript">
function evalGroup()
{
var group = document.radioForm.myRadio;
for (var i=0; i<group.length; i++) {
if (group[i].checked)
break;
}
if (i==group.length)
return alert("No Checkbox is checked");
alert("Radio Button " + (i+1) + " is checked.");
}
</script>
</head>
<body>
<form name="radioForm">
Checkbox 1: <input type="chechbox" name="myRadio" /><br />
Chechbox 2: <input type="chechbox" name="myRadio" /><br />
Chechbox 3: <input type="chechbox" name="myRadio" /><br />
Chechbox 4: <input type="chechbox" name="myRadio" /><br /><br />
<input type="button" value="Eval Group" onclick="evalGroup()" />
</form>
</body>
</html>
|
|
|
The Output is Shown Here:
|
Checkbox Validation
Example
<html>
<head>
<script type="text/javascript">
var myFlag = false
function initValue() {
myFlag = document.forms[0].sizes[0].checked;
}
function sh(form) {
for (var i = 0; i < form.sizes.length; i++) {
if (form.sizes[i].checked) {
break;
}
}
alert(form.sizes[i].value);
}
function setmyFlag(setting) {
myFlag = setting;
}
function exitMsg() {
if (myFlag) {
alert("exit message.");
}
}
</script>
</head>
<body>
<form >
<input type="checkbox" name="sizes" value="1" checked="checked" onclick="setmyFlag(true)" />1
<input type="checkbox" name="sizes" value="2" onclick="setmyFlag(false)" />2
<input type="checkbox" name="sizes" value="7" onclick="setmyFlag(false)" /&g <input type="checkbox" name="sizes" value="5" onclick="setmyFlag(false)" />5
t;5
<input type="button" name="Viewer" value="View" onclick="sh(this.form)" />
</form>
</body>
</html>
|
|
|
The Output is Shown Here:
|