Solved

Radio button Onclick submit form with validation

Posted on 2016-10-10
5
43 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
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.…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

932 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

12 Experts available now in Live!

Get 1:1 Help Now