Solved

Radio button Onclick submit form with validation

Posted on 2016-10-10
5
23 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
  • 3
  • 2
5 Comments
 
LVL 21

Expert Comment

by:Kim Walker
Comment Utility
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
Comment Utility
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 21

Accepted Solution

by:
Kim Walker earned 500 total points
Comment Utility
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
Comment Utility
Thank you so much Kim for helping. I really appreciate it.
0
 

Author Closing Comment

by:meldraper
Comment Utility
Thank you.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now