Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 4th, 2005, 5:52 AM   #1
raikkonen
Newbie
 
Join Date: Jun 2005
Posts: 29
Rep Power: 0 raikkonen is on a distinguished road
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">
raikkonen is offline   Reply With Quote
Old Sep 4th, 2005, 6:05 AM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Um... what?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Sep 4th, 2005, 6:14 AM   #3
raikkonen
Newbie
 
Join Date: Jun 2005
Posts: 29
Rep Power: 0 raikkonen is on a distinguished road
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?
raikkonen is offline   Reply With Quote
Old Sep 4th, 2005, 6:31 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Sep 4th, 2005, 7:11 AM   #5
raikkonen
Newbie
 
Join Date: Jun 2005
Posts: 29
Rep Power: 0 raikkonen is on a distinguished road
<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??
raikkonen is offline   Reply With Quote
Old Sep 4th, 2005, 9:34 AM   #6
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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.
coldDeath is offline   Reply With Quote
Old Sep 4th, 2005, 10:39 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Sep 4th, 2005, 11:14 AM   #8
proghelper
Newbie
 
Join Date: Jun 2005
Posts: 16
Rep Power: 0 proghelper is on a distinguished road
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
proghelper is offline   Reply With Quote
Old Sep 4th, 2005, 11:52 AM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Sep 4th, 2005, 12:43 PM   #10
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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.
coldDeath is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:58 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC