how to validate radiobutton, checkbox n dropdown list?
i am trying to validate radiobutton but to no avail.
this is the code that i m currently using to validate radiobutton:
<script language="JavaScript">
function checkForm()
{
var cgender;
with(window.document.msg)
{
cgender = gender;
}
if(trim(cgender .value) == '')
{
alert('Please select ur gender');
cgender .focus();
return false;
}
else
{
cgender.value = trim(cgender.value);
return true;
}
}
</script>
<body>
<form method="post" name="msg">
Gender:
<input name="gender" type="radio" value="male" />
Male
<input name="gender" type="radio" value="female" />
Female
<input type="submit" name="submit" value="Submit"onClick="return checkForm();">
when i click onto the submit button, nothing happens.
can someone tell me what is wrong with my code? n can someone give me an example on how to validate checkbox and also dropdown list?
Re: how to validate radiobutton, checkbox n dropdown list?
You shouldn't need to validate a radio option its either going to be one or the other just include the checked attribute for one of the options
HTML Code:
<input name="gender" type="radio" value="male" checked="true"/>
Re: how to validate radiobutton, checkbox n dropdown list?
i want it to validate. so that when i click onto the submit button, there will be pop-up message that says "Please select your gender"
Re: how to validate radiobutton, checkbox n dropdown list?
In that case cgender is an array so check
Code:
if(cgender[0].checked == false && cgender[1].checked == false){
}
Re: how to validate radiobutton, checkbox n dropdown list?
when i tried this code, there is a pop up error msg that say "please...gender" then it quickly redirect me to another page.
this is the code that i m doing right now. it did not validate the name validation part but instead, just redirect me to another page. can someone shine the light here? i want it to validate the name as well.
function checkForm()
{
var gender, fname;
with(window.document.msgform)
{
gender=ngender;
name = nfname;
if(gender[0].checked == false && gender[1].checked == false)
{
alert('Please specify your gender');
return false;
}
else if(fname.value == "")
{
alert('Please enter your first_name');
fname.focus();
return false;
}
else{
return true;
}
}
}
<form name = "form" onsubmit="return checkform();">
<input name="gender" type="radio" value="male" />
Male
<input name="gender" type="radio" value="female" />
Female</label>
First Name: <input name="name" type="text"/>
<input type="submit" name="submit" value="Submit">
Re: how to validate radiobutton, checkbox n dropdown list?
You might have to look at your code thoroughly. Variables were declared wrong, your form name is different, and there's no even a method and action. Here is the revised version:
Code:
<script>
function checkForm()
{
var ngender, fname;
with(window.document.msgform)
{
ngender = gender;
fname = name;
if(ngender[0].checked == false && ngender[1].checked == false)
{
alert('Please specify your gender');
return false;
}
else if(fname.value == "")
{
alert('Please enter your first_name');
fname.focus();
return false;
}
else{
return true;
}
}
}
</script>
<form name ="msgform" method=POST onsubmit="return checkForm();">
<input name="gender" type="radio" value="male" />
Male
<input name="gender" type="radio" value="female" />
Female</label>
First Name: <input name="name" type="text"/>
<input type="submit" name="submit" value="Submit">