Solved

Radio button Onclick submit form with validation

Posted on 2016-10-10
5
76 Views
Last Modified: 2016-11-25
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
Comment
Question by:meldraper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 22

Expert Comment

by:Kim Walker
ID: 41837099
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
 

Author Comment

by:meldraper
ID: 41837183
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
 
LVL 22

Accepted Solution

by:
Kim Walker earned 500 total points
ID: 41837202
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
 

Author Comment

by:meldraper
ID: 41837225
Thank you so much Kim for helping. I really appreciate it.
0
 

Author Closing Comment

by:meldraper
ID: 41901933
Thank you.
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question