Hmm..i just gave that a try, and it didn't work..Form still got submitted.
Main Topics
Browse All TopicsHi,
I'm using the following javascript to test if one of a set of radio buttons have been checked...
adv= -1
for (i=0; i<form.advice.length; i++) {
if (form.advice[i].checked) {
adv=i
}
}
if (adv == -1) {
alert ('Are you an independant financial adviser? Please select yes or no')
form.advice.focus()
return false
}
The actual validation part works fine but the focus() part seems to send the form off to the server instead of returning false. If I take out the focus() part, it returns false which is what I want it to do. How can I get the form to return false AND focus on the radio after validation?
Thanks!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I cant because its on our local server. Ok...all I am using is what you gave me just now. So i have..
<script language="javascript">
function chkFields() {
adv= -1
for (i=0; i<form.advice.length; i++) {
if (form.advice[i].checked) {
adv=i
}
}
if (adv == -1) {
alert ('Are you an independant financial adviser? Please select yes or no')
return false;
form.advice.focus() ;
}
}
</script>
Then:
form name="subscription" onSubmit="return chkFields()">
etc etc.
The focus works fine for all the other form elements. It just wont focus on the radio button.
I cant because its on our local server. Ok...all I am using is what you gave me just now. So i have..
<script language="javascript">
function chkFields() {
adv= -1
for (i=0; i<form.advice.length; i++) {
if (form.advice[i].checked) {
adv=i
}
}
if (adv == -1) {
alert ('Are you an independant financial adviser? Please select yes or no')
return false;
form.advice.focus() ;
}
}
</script>
Then:
form name="subscription" onSubmit="return chkFields()">
etc etc.
The focus works fine for all the other form elements. It just wont focus on the radio button.
I have a page that does this. Check out my amazing work ;-)
http://www.royalmail.com/q
The code you need is in there. I can extract it if you can't find it exactly.
your problem is that you are using onSubmit event. It does not prevent form from submission even if your data validation fails. Try using instead of "type=submit"
regular button,
<input type='button' value=submit onclick='chkFields()'>
and then manually submit
your form
script language="javascript">
function chkFields() {
adv= -1
for (i=0; i<form.advice.length; i++) {
if (form.advice[i].checked) {
adv=i
}
}
if (adv == -1) {
alert ('Are you an independant financial adviser? Please select yes or no')
form.advice.focus() ;
return false;
}
else {
document.form.submit();
return;
}
}
</script>
you overlooked one small detail:
form.advice.focus()
should be
form.advice[0].focus();
remember form.advice is an array containing the same number of radio buttons you have.
you didn't get the return false to work because there was an error before that.
when CUTTHEMUSIC reversed the order of the return and focus it only disabled the focus part since the return doesn't execute anything after it.
please try this, i hope it works.
Nothing seems to have happened here for a while so here's a fully working page. You should be able to extract what you want.
<html>
<head>
<script language="JavaScript" type="text/javascript">
var b, buttonvalue, everythingok
function validateform(form) {
var nogo = false
for (i=1;i<2;i++) {
if (!cycleButtons(form.whenbo
if (!cycleButtons(form.whenma
if (!cycleButtons(form.howman
if (form.username.value == "") {alert("Please enter your name");nogo=true;form.user
else {textvalidator(form.userna
if (everythingok == false) {alert("Please enter a name with only letters");nogo=true;form.u
}
if (nogo) {
return false;
}
}
function cycleButtons(buttonNum) {
buttonchecked = false
for (b = 0; b < buttonNum.length; b++) {
if (buttonNum[b].checked) {
buttonchecked = true;
}
}
if (buttonchecked == false) {return false}
else {return true}
}
function textvalidator(field) {
//do text validation for input box
}
</script>
</head>
<body>
<form method="post" action="mynewpage.asp">
<p>1. HM The Queen Mother was born on which day in 1900?:<br>
<input type="radio" name="whenborn" value="3">3rd August<input type="radio" name="whenborn" value="4">4th August<input type="radio" name="whenborn" value="5">5th August</p>
<p>2. HM The Queen Mother married HRH Duke of York in which year?<br>
<input type="radio" name="whenmarried" value="21">1921<input type="radio" name="whenmarried" value="22">1922<input type="radio" name="whenmarried" value="23">1923</p>
<p>3. In 1997, HM The Queen Mother fulfilled how many official engagements?<br>
<input type="radio" name="howmany" value="37">37<input type="radio" name="howmany" value="44">44<input type="radio" name="howmany" value="58">58</p>
<p>Name: <input type="text" name="username" size="30"></p>
<input type="submit" name="submitbutton" value="Submit" onclick="return validateform(this.form)">
</form>
</body>
</html>
Lisa, was this ever resolved? I gave code that works. You never gave any feedback so I don't know if you need any other changes made.
If you found a solution here then accept the relevent comment as an answer. We are here to help and leaving questions until they automatically get deleted won't help others if they want to use the PAQ's.
It's time to clean up this topic area and that means taking care of this question. Your options at this point are:
1. Award points to the Expert who provided an answer, or who helped you most. Do this by clicking on the "Accept Comment as Answer" button that lies above and to the right of the appropriate expert's name.
2. PAQ the question because the information might be useful to others, but was not useful to you. To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.
3. Ask Community Support to help split points between participating experts. Just comment here with details.
4. Delete the question because it is of no value to you or to anyone else. To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.
If you elect for option 2, 3 or 4, just post comment with details here and I'll take it from there. We also request that you review any other open questions you might have and update/close them. Display all your question history from your Member Profile to view details.
PLEASE DO NOT AWARD THE POINTS TO ME.
__________________________
Hi Experts:
In the event that the Asker does not respond, I would very much appreciate your opinions as to which Expert ought to receive points (if any) as a result of this question. Likewise, you can also suggest that I PAQ or delete the question.
Experts, please do not add further "answer" information to this question. I will be back in about one week to finalize this question.
Thank you everyone.
Moondancer :)
Community Support Moderator @ Experts Exchange
Business Accounts
Answer for Membership
by: dij8Posted on 2000-09-12 at 06:00:32ID: 4273830
The way I do this is to return false seperately from all other statements. Using your example it would work something like:
for (x=1;x<2;x++) {
nogo = false;
adv= -1
for (i=0; i<form.advice.length; i++) {
if (form.advice[i].checked) {
adv=i
}
}
if (adv == -1) {
alert ('Are you an independant financial adviser? Please select yes or no')
form.advice.focus()
nogo = true;
break;
}
}
if (nogo) {return false}
The extra loop (x) is so that you can break out of multiple fields validation.