SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Feb 2000
    Location
    Minnesota
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Why doesn't this Javascript radio button form validation work?

    Here's the function part:
    Code:
    function validateCheckBox(field, field2, msg) {
    	for (i=0;i<3;i++)
    	{
    		if (field[i].checked)
    		{
    			user_checked = field[i].value;
    			if (user_checked == 1 && field2.value.length < 0) {
    				alert(msg);
    				return false;
    				}
    				return true;
    		}
    	}
    }
    And then I have this part:
    Code:
       validateCheckBox(this.hospital_affiliated, this.shopper_hospital_name,
    	  'Error Generated')
    When the checked radio button returns the value of 1, I want to make sure that the "shopper_hospital_name" field is not left empty.

    Thanks!
    Keomed.com Webmaster
    www.keomed.com

  2. #2
    SitePoint Addict
    Join Date
    Mar 2002
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Why doesn't this Javascript radio button form validation work?

    Presumably you would fill it with some kind of default value.

    Nick
    All4YourPC for all your PC needs. Custom software.
    Websites. Web marketing. Training.
    http://members.aol.com/nickjc67

  3. #3
    SitePoint Member
    Join Date
    Nov 2002
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is

    Code:
    user_checked = field[i].value;
    definitely returning 1 as a number and not a string. In your If statement you are testing for a number so ensure that is what is being returned.

    vibes

  4. #4
    SitePoint Member
    Join Date
    Nov 2002
    Location
    Indiana
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you maybe need to change the first field to field1?

    function validateCheckBox(field, field2, msg)

  5. #5
    SitePoint Addict
    Join Date
    Feb 2000
    Location
    Minnesota
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got it to work, but have a question:
    1). Do I have the "return true;" in the right place?
    Code:
    function validateRadioBox(field, field2, msg) {
    	for (i=0;i<3;i++)
    	{
    		if (field[i].checked) {
    			user_checked = field[i].value;
    			if (user_checked == 1 && field2.value == "") {
    				alert(msg);
    				field2.focus();
    				field2.select();
    				return false;
    			}
    			return true;
    		}
    	}
    }
    Keomed.com Webmaster
    www.keomed.com

  6. #6
    SitePoint Member
    Join Date
    Nov 2002
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does your returning true or false depend on whether field[i] is checked.

    Because at the moment it will only return true if field[i] is checked AND user_checked != 1 OR field2.value != "".

  7. #7
    SitePoint Addict
    Join Date
    Feb 2000
    Location
    Minnesota
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vibes35, Thanks for your help.
    I figured it out the same time your replied back.
    I had a couple of return true;'s in the wrong place and that was causing some of my issues.

    I appreciate your and everyone elses help.
    Thanks!
    Keomed.com Webmaster
    www.keomed.com

  8. #8
    SitePoint Member
    Join Date
    Nov 2002
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no problem


Bookmarks

Posting Permissions

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