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.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    form validate , radio buttons

    Hello

    in "validateField" function radio is not exists and I don't know how can I add a validation rule for radio buttons.
    please help me .

    my code is attached.

    thank you
    Attached Files Attached Files

  • #2
    Regular Coder
    Join Date
    Nov 2009
    Posts
    247
    Thanks
    4
    Thanked 22 Times in 22 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>None</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">	
    
    	var labelIndex = 0;
    
    	function validate(radioGroupLabel,radioGroupName){		
    		
    	 	for (each in radioGroupName)
    			{
    			 var nRadioGroup = document.getElementsByName(each);
    			 for (i=0; i<nRadioGroup.length; i++)
    				{
    				 if (nRadioGroup[i].checked)
    					{
    					 radioGroupName[each] = 1;
    					}
    				}
    			}
    		radioGroupLabel[labelIndex].style.backgroundColor = "white";
    		labelIndex = 0;		
    		for (each in radioGroupName)
    			{			 
    			 if (radioGroupName[each] == 0)
    				{
    				 alert('You must choose from ' + radioGroupLabel[labelIndex].firstChild.data);
    				 radioGroupLabel[labelIndex].style.backgroundColor = "#ff6347";
    				 return false;
    				}
    			 labelIndex++;
    			}
    		alert('Thank you');
    		return true;
    	}
    
    	function init(){
    
    		var nForm = document.forms[0];		
    		var radioGroupLabel = [];
    		var radioGroupName = {};	
    		var nLabel = nForm.getElementsByTagName('label');
    		for (i=0; i<nLabel.length; i++)
    			{
    			 if (nLabel[i].className == "radio_group")
    				{
    				 radioGroupLabel[radioGroupLabel.length] = nLabel[i];				 
    				}
    			}		
    		var nInput = nForm.getElementsByTagName('input');
    		for (i=0; i<nInput.length; i++)
    			{
    			 if (nInput[i].type == "radio")
    				{				
    				 radioGroupName[nInput[i].name] = 0;
    				}
    			}	
    		nForm.onsubmit = function()
    			{
    			 return validate(radioGroupLabel,radioGroupName);
    			}	
    	}
    
    	navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);	
    
    </script>
    <style type="text/css">
    
    	 label {font-family: times; font-size: 12pt;}
    	.radio_group {padding-left: 5px; padding-right: 25px;}
    
    </style>
    </head>
    	<body>
    		<form action="" method="post">
    		  
    			<label class="radio_group">Group 1:</label>			
    				<label>AR: <input type="radio" name="radSafe1" value="AR"></label>
    				<label>Safe: <input type="radio" name="radSafe1" value="Safe"></label>
    				<label>No: <input type="radio" name="radSafe1" value="NO"></label>
    			<br>
    			<label class="radio_group">Group 2:</label>			
    				<label>AR: <input type="radio" name="radSafe2" value="AR"></label>
    				<label>Safe: <input type="radio" name="radSafe2" value="Safe"></label>
    				<label>No: <input type="radio" name="radSafe2" value="NO"></label>
    			<br>
    			<label class="radio_group">Group 3:</label>			
    				<label>AR: <input type="radio" name="radSafe3" value="AR"></label>
    				<label>Safe: <input type="radio" name="radSafe3" value="Safe"></label>
    				<label>No: <input type="radio" name="radSafe3" value="NO"></label>
    			<br><br>
    			<input type="submit" value="Submit">
    
    		</form>
    	</body>
    </html>


  •  

    Posting Permissions

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