Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 4 FirstFirst 1234 LastLast
Results 16 to 30 of 52
  1. #16
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Really appreciate your help guys, i've figured both out hehe took me alot of trial and error but i managed to get the javascript working for the above 2 validations!..

    How would i go about doing this. "Input device must allow user to choose either Male or Female but not both ?

    I've tryed doing it like this but nothing pops up ??

    Code:
    {
    	var returnVal = true;
    	var errorMsg = "";
    	
    		
    		if((!formtag.male[0].checked) && (!formtag.female[1].checked))
    		{
    			errorMsg += "Please choose one of the two! \n"; 
    }
    
    else if((formtag.male[0].checked) && (!formtag.female[1].checked)) 
    			{
    				errorMsg += "Thanks for choosing male\n"; 
    				returnVal = false;
    			}
    else if ((!formtag.male[0].check) && (formtag.female[1].checked))
    {
    	errorMsg += "Thanks for female \n"; 
    				returnVal = false;
    			}

  2. #17
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,869
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    Ummm...
    Code:
    <label><input type="radio" name="gender" value="male"/> Male </label>
    <label><input type="radio" name="gender" value="female"/> Female </label>
    Why do you need JavaScript for that?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  3. #18
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Ummm...
    Code:
    <label><input type="radio" name="gender" value="male"/> Male </label>
    <label><input type="radio" name="gender" value="female"/> Female </label>
    Why do you need JavaScript for that?
    For the assignment states i need javascript VALIDATION so the user can only pick one of the two using javascript, i know radio buttons can only choose 1 but i think for the assignment i'll be using checkboxes as you can't validate radio buttons using JAVASCRIPT

  4. #19
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,335
    Thanks
    13
    Thanked 348 Times in 344 Posts
    Quote Originally Posted by Johnny87auxsm View Post
    For the assignment states i need javascript VALIDATION so the user can only pick one of the two using javascript, i know radio buttons can only choose 1 but i think for the assignment i'll be using checkboxes as you can't validate radio buttons using JAVASCRIPT
    then the assignment doesn’t make sense. why would you go for the complicated custom script when default behaviour can do it better?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  5. #20
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,869
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    Then show your HTML <form> with the two checkboxes. Your use of [0] and [1] makes no sense at all to me, as it sits.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  6. #21
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm really confused, i emailed my unit co-ordinator and told him why would we validate female and male as they're already radio buttons. his reply was "Yes you are right in the case of checking only one option. However, the mandatory nature of the input needs to be validated using Javascript. " , didn't answer my question at all..

  7. #22
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Here is my html, basically first radio button is [0] second radio button is [1] or am i wrong?

    Code:
    <tr> <td><input type="checkbox" size="" id="male" name="male" /> Male</td>	
    		<td><input type="checkbox" size="" id="female" name="female" /> Female</td> </tr>

  8. #23
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,869
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    "mandatory" input means he wants you to check to make sure that indeed one of the buttons *WAS* checked. So I *think* that means he would accept radio buttons if you then ensured one of them was checked.

    But for the checkboxes, as you gave them, it's trivial:
    Code:
        function validateGender( )
        {
            return    document.getElementById("male").checked 
                   || document.getElementById("female").checked;
        }
    Why work harder than that?

    Oh...and size= is completely meaningless for a checkbox. What could it possibly mean?
    Last edited by Old Pedant; 05-02-2013 at 01:33 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  9. #24
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hey dude got it working just used some code from my textbook, i copied the code and changed the name from my form to do another validation but for some reason the 2nd bit of code isn't working, i'll paste it, quite odd the first works but second isn't? The second is checkbox where as the first is radio buttons which work..

    Code:
    THIS WORKS
    
     var booValid = true;
      var strErrorMsg = "";
      
      if ((!formtag.gender[0].checked) && (!formtag.gender[1].checked))
      
      {
      
      	strErrorMsg += "Must select Female or Male\n";
      	booValid=false;
      	
      	}
     
      if(!booValid)
      {
      
      alert(strErrorMsg);
      
      return booValid;
      }
      
      
      DOESNT WORK?
      
      var booValid = true;
      var strErrorMsg = "";
      
      if ((!formtag.agreed[0].checked))
      
      {
      
      	strErrorMsg += "Must eat\n";
      	booValid=false;
      	
      	}
     
      if(!booValid)
      {
      
      alert(strErrorMsg);
      
      return booValid;
      }

  10. #25
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Actually i think it has something to do with my whole javascript? Anyway i can send you my file via email so u can just debug it, any bit of code i put after the WORKING one which i posted above doesn't seem to work when its 100% correct?

  11. #26
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,869
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    One more time, SHOW YOUR HTML.

    I would *ASSUME* the error is in the bogus use of [0] in this code:
    Code:
     if( !formtag.agreed[0].checked )
    but who can tell when you refuse to show the HTML you are using?

    Do you understand why the [0] might be bogus?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  12. #27
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,869
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    Copying code is an utter waste of time when you don't understand how it works.

    It just means that the next time you have a problem that doesn't happen to match what you copied, you won't have any idea what to do.

    You need to *understand* what the code is doing and why it works (or doesn't work).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  13. #28
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i understand the code just not exactly sure why it isn't working, the code above it is exactly the same which pops up with an alert.. here is my HTML + CODE

    <input type="checkbox" id="agreed" name="agreed"/>

  14. #29
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Nevermind got it working ! trial and error grrr lol, making some good progress with javascript. One thing i noticed is if you have one error within your code the whole javascript doesn't seem to work!! , can be a huge PITA...

    I'm stuck on one of the hardest ones i've come across thus far within the assignment, basically when the user inputs value into a textbox that value must appear NON-editable in another checkbox down below ? Any idea how to start?

  15. #30
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,869
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    ??? "in another checkbox"? You sure you don't mean in another textbox?

    Why is that hard?

    To make a textbox non-editable, just use html.
    Code:
    <input name="whatever" readonly="readonly" />
    Surely by now you know how to copy a value from one field to another?

    You know how to read field values. I assume you know how to change them?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


 
Page 2 of 4 FirstFirst 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •