?
Solved

Radio button Onclick submit form with validation

Posted on 2016-10-10
5
Medium Priority
?
89 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 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
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 …
Suggested Courses

771 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