![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4
![]() |
JavaScript validate form(option button)
I use this JavaScript to validate my form; the script works perfectly except when I tried making it work with option button, I tried sth like but it aint working
if (form.field_name.checked == false) This is my js <script language="JavaScript" type="text/javascript">
<!--
function checkform (form)
{
if (form.field_name.value == "")
{
alert( "a msg error here");
form.field_name.focus();
return false ;
}
return true ;
}
//-->
</script>
__________________
Personal Portfolio TecBrain Support Forum Linux VS Windows ... Dont Even Think of it .. Distribution: Slackware if (OS==Linux) return success There are 10 kinds of people, those who can read binary numbers and those who can't. |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
If you're referring to a radiobutton group, then remember that all buttons in a group have the same name. Each button is distinguished by its position in an array representing the group. In your example you would need to refer to a member thusly:
if (form.field_name [0].checked == false) Normally, I'm going to cycle through and check buttons in multiple forms, so I make an object variable for a group, run through the group, set the variable for another group, run through that group, etc., until I'm done. The HTML stuff:
<input name="checklist" type="radio" value="catalog"
onClick="switchCart ('cat');" >
<input name="checklist" type="radio" value="cart"
defaultChecked checked onClick="switchCart ('cart');" >
The script stuff:
var cObj = document.forms ["headForm"].checklist;
if (cObj [0].checked) blah blah....
...
...
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4
![]() |
by specifying the position i will have to check all of them then!!, so this is what i did:
function checkform(form)
{
if ((form.same_room[0].checked == false) && (form.same_room[1].checked == false))
{
alert( "You Choose");
form.same_room[0].focus();
return false ;
}
return true ;
}and this is the html part: <td class='style8'> 1) Do you mind if your exam is scheduled in the same room with another exam?<br /><br /> <input type='radio' name='same_room' value='yes' >Yes<br /> <input type='radio' name='same_room' value='no'>No<br /><br /> <i>If you chose YES, please give your reasons. If no reason is provided the request will be rejected:</i><br /><br /> <textarea rows='6' name='same_room_reason' cols='47'></textarea> <br /> </td> But stilll aint working!!
__________________
Personal Portfolio TecBrain Support Forum Linux VS Windows ... Dont Even Think of it .. Distribution: Slackware if (OS==Linux) return success There are 10 kinds of people, those who can read binary numbers and those who can't. |
|
|
|
|
|
#4 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4
![]() |
thats what i understood.. what i meant is:
form.same_room[0].checked would refer to the first option button, Right?
__________________
Personal Portfolio TecBrain Support Forum Linux VS Windows ... Dont Even Think of it .. Distribution: Slackware if (OS==Linux) return success There are 10 kinds of people, those who can read binary numbers and those who can't. |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4
![]() |
I got it working DaWei thanks for your tip
![]() else if ((document.getElementsByName('same_room')[0].checked==true) && (document.update.same_room_reason.value == ""))
{
alert( "Is your exam in the same room? if Yes then please give your reasons.");
document.update.same_room_reason.focus();
return false;
}
__________________
Personal Portfolio TecBrain Support Forum Linux VS Windows ... Dont Even Think of it .. Distribution: Slackware if (OS==Linux) return success There are 10 kinds of people, those who can read binary numbers and those who can't. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|