...

View Full Version : validate radiobutton



mrjameer
06-17-2007, 06:47 PM
hi


i have 3 radio buttons and 3 textboxes on a form.



<input type="radio" name="radiogroup1" value="others"><b>Other Needs</b>
<input type="radio" name="radiogroup1" value="courses"><b>Need Courses</b>
<input type="radio" name="radiogroup1" value="money">
<b>Need Money</b>

Enter Name<input type="text" name="ur_name">
Enter Father Name<input type="text" name="father_name">
Enter Your Age<input type="text" name="age">

what i want is if a user clicks on money radiobutton and enter his name and father name and without filling the age textbox press submit button then it display alert to fill the age textbox.if clicks any of other two radiobuttons,dont display any alert.any of your help will be surely appreciated.

iam using the following code to validate the radiobutton(if a user dont clicks any radiobutton display alert to select it)
but i dont know how i can check that when money radiobutton is clicked the age textbox should be filled.it should not be blank


thanks
mrjameer

<script language="javascript">
function validate()
{
var i=0;
var chkcount;
chkcount=0;
for (i=0;i<document.frm.elements.length;i++)
{
if ( document.frm.elements[i].type=="radio" && document.frm.elements[i].name=="radiogroup1" && document.frm.elements[i].checked==true)
{
chkcount=chkcount+1
}
}
if (chkcount==0)
{
alert(" Please Select Your Need");
return false;
}
return true;
}
</script>

Philip M
06-17-2007, 07:41 PM
Try this:-

<FORM name = "frm">

<input type="radio" name="radiogroup1" value="others"><b>Other Needs</b><br>
<input type="radio" name="radiogroup1" value="courses"><b>Need Courses</b><br>
<input type="radio" name="radiogroup1" value="money"><b>Need Money</b><br>

Enter Name<input type="text" name="ur_name">
Enter Father Name<input type="text" name="father_name">
Enter Your Age<input type="text" name="age"><br><br>

<input type = "button" name = "valbuttton" value = "Click to proceed" onclick = "validate()">

<script language="javascript">
function validate() {
var i=0;
var chkcount= 0;

for (i=0;i<document.frm.elements.length;i++) {
if ( document.frm.elements[i].type=="radio" && document.frm.elements[i].name=="radiogroup1" && document.frm.elements[i].checked==true) {
chkcount=chkcount+1;
}
}

if (chkcount == 0) {
alert(" Please Select Your Need");
return false;
}

if ((document.frm.radiogroup1[2].checked) && (document.frm.age.value == "")) {
alert ("Age required");
return false;
}

return true;
}
</script>

</FORM>

You need additional validation to check that the entries into the textboxes are sensible (e.g. age is a number within a sensible range).

mrjameer
06-17-2007, 08:36 PM
hi Philip M

thanks for your reply.

i want to put a submit button instead of normal button.

so that what ever the information entered is stored in mysql table.how i can replace the normal button with submit button(<input type=submit name=submit>).

thanks
mrjameer.

Philip M
06-18-2007, 08:53 AM
<input type = "submit" name = "submitbutton" value = "Click to submit" onclick = "return validate(this)">

Do not give your submit button the name 'submit'. It can confuse the browser.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum