Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alert box based on radio button selection 1

Status
Not open for further replies.

LOW

Programmer
Sep 27, 2000
45
US
I want an alert (reminder) box to pop up when the "no" radio button is selected on my form. Currently what happens is that the alert box pops up when the "no" button is selected and also when the "yes" button is selected. I dn't want it to pop up when "yes" is selected. Evidentally, I'm overlooking something. The following is my code. Any suggestions would be appreciated.

<script language="Javascript" type="text/javascript">
<!--
function isGood(form) {
if (!document.formcheck.followup.yes) {
alert("Please enter your phone number or email address so that we can contact you.");
return false;
}
return true;
}
// -->
</script>
====================================
Here's the code for the radio button I'm referring to:

<tr valign="top">
<td colspan="2"><strong>May we contact you for follow-up?</strong>
<label for="Follow-up"><input type="radio" name="followup" value="yes" id="Follow-up"> Yes</label>&nbsp;&nbsp;
<label for="No follow-up"><input type="radio" name="followup" value="no" id="No follow-up" checked> No </label>
</td>
</tr>

 
Try this:

Code:
function isGood(form) {
    var the_rad = document.forms['the_form_name'].elements['followup'];
    if (the_rad[1].checked) {
        alert("Please enter your phone number or email address so that we can contact you.");
        return false;
    }
    return true;
}

*cLFlaVA
----------------------------
Breaking the habit...
 
Code:
<script language="Javascript" type="text/javascript">    
<!--
function isGood(form) {
  if (document.formcheck.followup[1].checked) {
    alert("Please enter your phone number or email address so that we can contact you.");
    return false;
  }
  return true;
}

followup[0] is the first radio button, followup[1] is the second, .checked is true if the button is checked, false otherwise.

--Chessbot

"DON'T PANIC
 
why do you need to pass the form to [tt]isGood()[/tt]?

--Chessbot

"DON'T PANIC
 
Thank you for all the assistance. But now I have another problem. When the "yes" radio is selected, the alert box pops up (which is what I want) but it's preventing me from actually submitting the form. Not sure what to do to fix this.

=======================================================

<script language="Javascript" type="text/javascript">
<!--

function isGood() {
if (document.formcheck.contact[0].checked) {
alert("Please enter your phone number or email address so that we can contact you.");
return false;
}
return true;
}

// -->

</script>
 
Yeah, because you're returning false if the Yes button is checked.

If you return true there, the alert will display, and then the form will submit anyway...

*cLFlaVA
----------------------------
Breaking the habit...
 
off topic:

cLFlaVA: what habit are you breaking? Smoking? I'm 3 years and almost 7 months off myself. If that's what it is: GOOD FOR YOU! It's been pretty easy for me, but I never stop missing it. Whenever I think of having one, though, I think of how long it's been and how I don't want to have to start over from time t=0 again. Good luck!

P.S., I wouldn't post an off-topic post like this if I didn't think the thread was closed. It appears all questions have been asked and answered. Sorry if I've interrupted the flow of things.
 
Thank you cLFlaVA. As you can tell, I'm a newbie at this. I have another question. How do I get the alert box to pop up prior to clicking the submit button. I would like the user to have the opportunity to go back to fill in the phone or email fields prior to hitting the submit button.

 

LOW-
Call that function in the radio button's onchange event. It would be a good idea to put it in both radio buttons.






LookingForInfo-
While your 3 years, 7 months off the stick is inspirational, I don't smoke. And by "smoke" I mean, I don't buy my own packs of cigarettes. Once every few months I'll have one with a friend, but that's really all.

Breaking the Habit is my current favorite song, by Linkin Park.

:)

*cLFlaVA
----------------------------
Breaking the habit...
 
LOW:

the purpose of having the function return 'false' and stopping the submit is to do form validation without allowing submit to occur. This is expected behavior by most online form users.

Putting the validation code (checking for checked radio buttons) at the time of form submission and returning 'false' in the event something is not right is pretty much the norm.

When 'false' is returned, the form is not submitted and the user then has the opportunity to fix whatever errors he/she made. Then, they hit submit again, the validation code checks out, the function returns 'true,' and the form is submitted.

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top