By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,816 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,816 IT Pros & Developers. It's quick & easy.

validate <input type="radio">

P: 17
I'm working on an assignment for school where we're working with JavaScript (I'm only allowed to use JavaScript) to validate a form for a payment page. It's my first time working with JavaScript so and I feel a bit lost..i need to link htmp radio buttons and javascript with a Calculate button


thml code

Expand|Select|Wrap|Line Numbers
  1. <td><input type="text" id="text7" name="fexpirydate" placeholder="22/08/2030" maxlength="30">
  2.         <tr/>
  3.         <tr>
  4.         <td><input type="radio" name="gender" value="Cash" checked><strong>Cash</strong></td>
  5.         </tr>
  6.         <tr>
  7.         <td><input type="radio" name="gender" value="Ecocash" checked><strong>Ecocash</strong></td>
  8.         </tr>
  9.         <tr>
  10.         <td><input type="radio" name="gender" value="Swipe" checked><strong>Swipe</strong></td>
  11.         </tr><br><br><br>
6 Days Ago #1
Share this Question
Share on Google+
3 Replies


gits
Expert Mod 5K+
P: 5,318
what exactly should happen in the program here and what have you done so far to solve it?
6 Days Ago #2

P: 2
Expand|Select|Wrap|Line Numbers
  1. <form name="form1" action="#" onsubmit="return validateForm()" method="post"> 
  2.     First time visitor?:<br/>
  3.             <label for="s1">Yes</label>
  4.             <input type="radio" id="radio1" name="yesno" value="1"/>
  5.             <br/>
  6.             <label for="s2">No</label>
  7.             <input type="radio" id="radio2" name="yesno" value="2"/>
  8.  
  9.             <br/>       
  10.  
  11.     <input type="submit" value="Submit"><br/>
  12.     </form>
  13.  


Expand|Select|Wrap|Line Numbers
  1. function validateForm() {
  2.     var radios = document.getElementsByName("yesno");
  3.     var formValid = false;
  4.  
  5.     var i = 0;
  6.     while (!formValid && i < radios.length) {
  7.         if (radios[i].checked) formValid = true;
  8.         i++;        
  9.     }
  10.  
  11.     if (!formValid) alert("Must check some option!");
  12.     return formValid;
  13. }
1 Days ago #3

gits
Expert Mod 5K+
P: 5,318
throwing out some code without knowing what the exact problem is is plain guesswork. While the OP is already asking because he/she is lost - with no further elaboration of that shown code it probably even wont help much - the answer could even be 42.

Besides that its pretty sub-optimal to do validations like that - since it requires DOM operations. its much more optimal when using events to update a state - you will see that when you have pages with lots of elements that you are validating the way you propose here.
22 Hours Ago #4

Post your reply

Sign in to post your reply or Sign up for a free account.