[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 112
  • Last Modified:

Radio button Onclick submit form with validation

I have an RSVP form and I'd like to submit the form onclick if they click the NO radio button but I also want them to be required to add their name.  How can I do both, validate the name field AND submit the form when they click the NO radio button.  Many thanks in advance.
0
meldraper
Asked:
meldraper
  • 3
  • 2
1 Solution
 
Kim WalkerWeb Programmer/TechnicianCommented:
Assuming that their name is one of the available fields on the form, the onclick event would first check that field to see if it contains characters. If not, it would alert the user to enter a name before clicking the no radio button and reset the no radio button to uncheck it. If the name field contains characters, you can then submit the form via javascript.

There may be an issue, though, with resetting the radio button to uncheck it. I believe that once one radio button in a set is checked, the only change allowed is to select another radio button. The solution to this is to create a third option, maybe call it "none" and hide it. Then you can check the hidden "none" button to deselect the "no" button. This also requires that the form submit validation be configured to fail is "none" is selected.
0
 
meldraperAuthor Commented:
Thanks Kim!  I definitely need more info though.  Right now I have the attend field and the name field as required and just didn't require the others.  It's ok, but I would need some code.  I have the onclick set to submit if they click no but it doesn't validate the name field, it just submits even if the name field is blank.  How do I code that?  Thanks again.
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
This expert suggested creating a Gigs project.
This forum is typically for troubleshooting code and answering questions rather than supplying complete customized code. There's another section of E-E for that type of help called E-E Gigs.

With that said, even though this may be a simple project, we would have to be able to see the HTML and existing javascript in order to determine exactly what needs to be coded. Can you post a link to the page with the RSVP form?

Does the name field have an id attribute? If so you can access the content of that field using document.getElementById.
document.getElementById('nameFieldId').value;

Open in new window

You would need to modify your existing code to add an if-else conditional statement.
if (document.getElementById('nameFieldId').value.trim() === "") {
    alert('You must enter your name before selecting No.');
    return false;
} else {
    // your existing code to submit the form would go here
}

Open in new window

0
 
meldraperAuthor Commented:
Thank you so much Kim for helping. I really appreciate it.
0
 
meldraperAuthor Commented:
Thank you.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now