![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Posts: 29
Rep Power: 0
![]() |
radio button
how do i validate 2 different group name by not using hard codeing?
here's my solutin however i couldnt get it working. pls advice. thanks in advance. <SCRIPT LANGUAGE='JavaScript'> function checkrbtn(form) { var radio = new Array() radio[0] = mc2 radio[1] = truefalse4 var myOption= -1; for (z=0; z<=radio.length; z++){ for (it=0; it<radio[z].length; it++) { if (radio[it].checked) { myOption = it; } } if (myOption == -1) { alert("You must select a radio button"); return false; } </SCRIPT> <form> <INPUT type='radio' value='mcq1' name='mc2' onClick=document.test.mc2.value='mcq1'> opt1<br> <INPUT type='radio' value='mcq2' name='mc2' onClick=document.test.mc2.value='mcq2'> opt2<br> <INPUT type='radio' value='mcq3' name='mc2' onClick=document.test.mc2.value='mcq3'> opt3<br> <INPUT type='radio' value='mcq4' name='mc2' onClick=document.test.mc2.value='mcq4'> opt4<br> <br> <INPUT type='radio' name='truefalse4' value ='true' onClick=document.test.truefalse4.value='true'> True <input type='radio' name='truefalse4' value ='false' onClick=document.test.truefalse4.value='false'> False<br> <input type="submit" onsubmit="return checkrbtn"> |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Um... what?
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jun 2005
Posts: 29
Rep Power: 0
![]() |
hmmm in my html coding, there is two grp of radio btns rite? i would like to validate them. how would i need to code it so that it is not hard coded?
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I'm not sure what you mean by "hard coded" and "validate". Why don't you just specify a default button? Then one or another will always be checked. If that's what you mean by "hard coded validation", what's wrong with that? If you mean, "How do I tell which one is checked?", please say so.
Incidentally, please use code tags around your code. It drives fewer potential readers/respondents away.
__________________
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 |
|
Newbie
Join Date: Jun 2005
Posts: 29
Rep Power: 0
![]() |
<html><title></title><head>
<SCRIPT LANGUAGE='JavaScript'> function checkrbtn(form) { var radio = new Array() radio[0] = mc2 radio[1] = truefalse4 var myOption= -1; for (z=0; z<=radio.length; z++){ for (it=0; it<radio[z].length; it++) { if (radio[it].checked) { myOption = it; } } if (myOption == -1) { alert("You must select a radio button"); return false; } </SCRIPT> </head><body> <form name="test"> <INPUT type='radio' value='mcq1' name='mc2' onClick=document.test.mc2.value='mcq1'> opt1<br> <INPUT type='radio' value='mcq2' name='mc2' onClick=document.test.mc2.value='mcq2'> opt2<br> <INPUT type='radio' value='mcq3' name='mc2' onClick=document.test.mc2.value='mcq3'> opt3<br> <INPUT type='radio' value='mcq4' name='mc2' onClick=document.test.mc2.value='mcq4'> opt4<br> <br> <INPUT type='radio' name='truefalse4' value ='true' onClick=document.test.truefalse4.value='true'> True <input type='radio' name='truefalse4' value ='false' onClick=document.test.truefalse4.value='false'> False<br> <input type="submit" onsubmit="return checkrbtn"> </body> </html> this is the code with tags. currently i am doing a project on writing question into html format from java coding. the java end has been done, and the user will be able to generate question from the java application and publish into html format. now i am doing the validation to check the radio btns(whether is it check?) when the user submit the html page. any advice how should i get the radio btns name?? |
|
|
|
|
|
#6 |
|
Expert Programmer
|
Firstly put the tags around your code when posting.
Secondly, if you have made a java application, a bit of javascript isn't going to be hard and you should know what you are talking about. Thirdly search google Fourthly use PHP for form handling. EDIT: also maybe you think java == javascript?
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
When we speak of "code tags" we refer to the entities which place your code into a box and preserve its indentation, not to html tags in your work. Please read the forum rules/faq and the "How to Post a Question" material.
coldDeath has a germane question: are you confusing Java with Javascript? They are far from the same thing. Normally, when you submit your form your browser generates a new request to the server (which disconnected after supplying your original page). Any form info is sent along with the request in accordance with your specifications, of which you currently have none. I presume you are just trying to validate the form contents without actual submittal. Read some of the numerous Javascript tutorials available. "Javascript" and "radio button" are good search terms. When you actually decide to submit the form, it will be handled by some mechanism on the server end. This might be PHP, Perl, or any number of other things. The server will typically then return a new page to the browser. At this point you need to absorb some basic information about the client/server paradigm, the http protocol, and so forth.
__________________
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 |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Jun 2005
Posts: 16
Rep Power: 0
![]() |
Hi ,
Check this code. Its working. <html> <head> <SCRIPT LANGUAGE='JavaScript'> function checkrbtn() { var radio1 = document.forms[0].mc2; var radio2 = document.forms[0].truefalse4; var myOption1= -1; var myOption2= -1; for (var z1=0; z1<radio1.length; z1++){ if (radio1[z1].checked) { myOption1 = z1; break; } } for (var z2=0; z2<radio2.length; z2++){ if (radio2[z2].checked) { myOption2 = z2; break; } } if ((myOption1 == -1) ||(myOption2 == -1)){ alert("You must select a radio button"); return false; } } </SCRIPT> </head> <body> <form> <INPUT type='radio' value='mcq1' name='mc2' onClick=document.test.mc2.value='mcq1'> opt1<br> <INPUT type='radio' value='mcq2' name='mc2' onClick=document.test.mc2.value='mcq2'> opt2<br> <INPUT type='radio' value='mcq3' name='mc2' onClick=document.test.mc2.value='mcq3'> opt3<br> <INPUT type='radio' value='mcq4' name='mc2' onClick=document.test.mc2.value='mcq4'> opt4<br> <br> <INPUT type='radio' name='truefalse4' value ='true' onClick=document.test.truefalse4.value='true'> True <input type='radio' name='truefalse4' value ='false' onClick=document.test.truefalse4.value='false'> False<br> <input type="button" onClick="javascript:checkrbtn()" value="Click"> </body> </html> John Programming Assignment Help / Project Help |
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Don't be infantile: listen up, pay attention: USE CODE TAGS AROUND YOUR CODE. Anything else is being rude to the other members of the forum who read your posts.
__________________
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 |
|
|
|
|
|
#10 |
|
Expert Programmer
|
And you didn't even think to say thank you to DaWei who spent time making an informative post. I bet you didn't even read it.
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|