|
|
Subject:
Validation of a combo of radio button and text field
Category: Computers > Programming Asked by: ledelboy-ga List Price: $10.00 |
Posted:
30 Apr 2004 21:12 PDT
Expires: 30 May 2004 21:12 PDT Question ID: 339237 |
At the end of my form I have two radio buttons: Yes and No. When the user selects NO, will be linked on submit to NO.HTML When the user selects YES, she will be linked to YES.HTML. Now comes the hard part: If she chooses YES she must also fill a Text Field QUANTITY with the number of units she wants. What I need is a little piece of javascript that forbids the user to select NO and input a number in QUANTITY, and that forces her to input a number in QUANTITY if she chooses yes. |
|
Subject:
Re: Validation of a combo of radio button and text field
Answered By: mmastrac-ga on 01 May 2004 08:40 PDT Rated: |
Hi ledelboy! The following HTML will accomplish what you are looking for. It places an "onClick" handler on the radio buttons to run the verification routines. Each verification routine checks for either the presence or absence of a value in the "quantity" field. If you require any additional verification checks, let me know. The following code has been tested in Mozilla Firefox 0.8 and IE 6.0. --- 8< --- <html> <body> <script> <!-- function unselectAll() { // Uncheck both YES and NO document.getElementById( 'rbYes' ).checked = false; document.getElementById( 'rbNo' ).checked = false; } function verifyYes() { var txtQuantity = document.getElementById( 'txtQuantity' ); if (txtQuantity.value == null || txtQuantity.value.length == 0) { alert( "Please enter a value for quantity." ); unselectAll(); } else { document.getElementById( 'frmSubmit' ).action = 'yes.html'; document.getElementById( 'frmSubmit' ).submit(); } } function verifyNo() { var txtQuantity = document.getElementById( 'txtQuantity' ); if (txtQuantity.value == null || txtQuantity.value.length == 0) { document.getElementById( 'frmSubmit' ).action = 'no.html'; document.getElementById( 'frmSubmit' ).submit(); } else { alert( "Please do not enter a value for quantity." ); unselectAll(); } } --> </script> <form id="frmSubmit" method="GET"> Quantity: <input id="txtQuantity" type="text" name="QUANTITY"> <br> Yes: <input id="rbYes" onClick="verifyYes()" type="radio" name="CHOICE" value="YES"> <br> No: <input id="rbNo" onClick="verifyNo()" type="radio" name="CHOICE" value="NO"> <br> </form> <script> <!-- // Unselect YES and NO unselectAll(); --> </script> </body> </html> --- 8< --- | |
|
ledelboy-ga
rated this answer:
and gave an additional tip of:
$3.00
Five stars because the Researcher went the extra mile (or foot). He/she gave me two versions. The second one was right on the money. Thnx. |
|
There are no comments at this time. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |