SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    validating radio buttons w/ javascript

    Im trying to make it so when the user submits a form, my script checks to see if at least one radio button within a group is selected. Ive been using the below code for my text boxes and it works great:

    if (form.q5c.value == "") {
    alert("Please enter estimated hours per week");
    form.q5c.focus()
    return false; }

    Any help would be great!

  2. #2
    I am the night... bman's Avatar
    Join Date
    Mar 2001
    Location
    In a van... down by the river.
    Posts
    366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is one i wrote awhile back to validate checkboxes, it should work for radiobuttons also. unless they work differently then i think they do.

    Code:
    function validate()
    //checkbox vailidator version 1.0\\
    //-=:Bman:=-\\ 
    //change FORMNAME to the name of your form
    //change RADIONAME to the name of your Radio buttons
    {	
    	var check_num = document.FORMNAME.RADIONAME.length
    	var howmanycheck = 0
    	
    	for (var i = 0; i < check_num; i++)		
    	{
    		if(document.FORMNAME.RADIONAME(i).checked)
    		{
    		howmanycheck = howmanycheck + 1
    		}			
    	}
    	if(howmanycheck == 0)
    	{
    	alert("You must choose atleast one radio button.");
    	document.FORMNAME.RADIONAME(0).focus();
    	return false
    	}	
    
    }
    you can take out the comments and stuff if you want.
    Last edited by bman; Oct 16, 2001 at 12:10.
    Some say the world will end in fire, Some say in ice.
    From what I've tasted of desire
    I hold with those who favor fire.

  3. #3
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The following was cut from the "Validating Forms" script/tutorial at my site (GrassBlade) where you can find complete discussion of the code. (The script also demonstrates how to validate for all types of form elements)


    function chkForm(formObj)
    {
    var isOK = notChecked = true;
    with (formObj)
    {
    for (i = 0; i < length ; i++)
    {
    if (elements[i].type == 'radio')
    {
    radName = elements[i].name;
    notChecked = true;
    do
    {
    if (elements[i].checked)
    notChecked = false;
    i++;
    } while (elements[i].name == radName);
    --i;
    if(notChecked)
    {
    alert("You forgot to pick a selection for "+ radName + " element " + i );
    isOK = false;
    }
    }
    }
    }
    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  4. #4
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Here is the code that I have been trying to get to work. group9 is the name of my set of radio buttons...

    if (form.group9.value != "Yes") And (form.group9.value != "No") {
    alert("Please answer question 9");
    form.group9.focus()
    return false; }

    Still pulling up errors with this, any help is useful...thanks...

  5. #5
    I am the night... bman's Avatar
    Join Date
    Mar 2001
    Location
    In a van... down by the river.
    Posts
    366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you post the code for the radio button part of the form that you are trying to get to validate?
    Some say the world will end in fire, Some say in ice.
    From what I've tasted of desire
    I hold with those who favor fire.

  6. #6
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    <b>9. Have you used Atlass II+?</b><br>
    <input type="radio" value="Yes" name="group9">Yes<br>
    <input type="radio" value="No" name="group9">No</p>

  7. #7
    I am the night... bman's Avatar
    Join Date
    Mar 2001
    Location
    In a van... down by the river.
    Posts
    366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this:
    Code:
    if((!document.form.group9(0).checked)&&(!document.form.group9(1).checked))
     { 
    alert("Please answer question 9"); 
    form.group9(0).focus() 
    return false; }
    Some say the world will end in fire, Some say in ice.
    From what I've tasted of desire
    I hold with those who favor fire.

  8. #8
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Im getting the error "document.form is null or not an object".. any ideas??

  9. #9
    I am the night... bman's Avatar
    Join Date
    Mar 2001
    Location
    In a van... down by the river.
    Posts
    366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you post the first part of your form tag.. just the <form...> part.
    Some say the world will end in fire, Some say in ice.
    From what I've tasted of desire
    I hold with those who favor fire.

  10. #10
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    sure can...

    <form onSubmit="return validate(this)" method="POST" action="addnew.asp">

  11. #11
    I am the night... bman's Avatar
    Join Date
    Mar 2001
    Location
    In a van... down by the river.
    Posts
    366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, change it to this

    <form onSubmit="return validate(this)" method="POST" action="addnew.asp" name="form">
    Some say the world will end in fire, Some say in ice.
    From what I've tasted of desire
    I hold with those who favor fire.

  12. #12
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    once again sitepoint members come through to help again. Your the man!! Thanks alot bman!! I appreciate it...

  13. #13
    I am the night... bman's Avatar
    Join Date
    Mar 2001
    Location
    In a van... down by the river.
    Posts
    366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no problem man, i have been there too.
    Some say the world will end in fire, Some say in ice.
    From what I've tasted of desire
    I hold with those who favor fire.


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
  •