Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-09-2007, 04:50 PM   PM User | #1
NiteOwl
New Coder

 
Join Date: Aug 2006
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
NiteOwl is an unknown quantity at this point
Question Form Validation for Radio Buttons

I found a PHP Form Generator, but, it does not appear to Validate Radio fields.

I do not have one "Selected" when the form is displayed to ensure it is marked.

I do not see a routine for radio fields in the code.
Can someone help me with the needed code.
Code:
	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
	<html>
	<head>
		<title>pay type - created by phpFormGenerator</title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link href="style.css" rel="stylesheet" type="text/css">

	    <!-- expand/collapse function -->
	    <SCRIPT type=text/javascript>
		<!--
		function collapseElem(obj)
		{
			var el = document.getElementById(obj);
			el.style.display = 'none';
		}


		function expandElem(obj)
		{
			var el = document.getElementById(obj);
			el.style.display = '';
		}


		//-->
		</SCRIPT>
		<!-- expand/collapse function -->


		<!-- expand/collapse function -->
		    <SCRIPT type=text/javascript>
			<!--

			// collapse all elements, except the first one
			function collapseAll()
			{
				var numFormPages = 1;

				for(i=2; i <= numFormPages; i++)
				{
					currPageId = ('mainForm_' + i);
					collapseElem(currPageId);
				}
			}


			//-->
			</SCRIPT>
		<!-- expand/collapse function -->


		 <!-- validate -->
		<SCRIPT type=text/javascript>
		<!--
			function validateField(fieldId, fieldBoxId, fieldType, required)
			{
				fieldBox = document.getElementById(fieldBoxId);
				fieldObj = document.getElementById(fieldId);

				if(fieldType == 'text'  ||  fieldType == 'textarea'  ||  fieldType == 'password'  ||  fieldType == 'file'  ||  fieldType == 'phone'  || fieldType == 'website')
				{	
					if(required == 1 && fieldObj.value == '')
					{
						fieldObj.setAttribute("class","mainFormError");
						fieldObj.setAttribute("className","mainFormError");
						fieldObj.focus();
						return false;					
					}

				}


				else if(fieldType == 'menu'  || fieldType == 'country'  || fieldType == 'state')
				{	
					if(required == 1 && fieldObj.selectedIndex == 0)
					{				
						fieldObj.setAttribute("class","mainFormError");
						fieldObj.setAttribute("className","mainFormError");
						fieldObj.focus();
						return false;					
					}

				}


				else if(fieldType == 'email')
				{	
					if((required == 1 && fieldObj.value=='')  ||  (fieldObj.value!=''  && !validate_email(fieldObj.value)))
					{				
						fieldObj.setAttribute("class","mainFormError");
						fieldObj.setAttribute("className","mainFormError");
						fieldObj.focus();
						return false;					
					}

				}



			}

			function validate_email(emailStr)
			{		
				apos=emailStr.indexOf("@");
				dotpos=emailStr.lastIndexOf(".");

				if (apos<1||dotpos-apos<2) 
				{
					return false;
				}
				else
				{
					return true;
				}
			}


			function validateDate(fieldId, fieldBoxId, fieldType, required,  minDateStr, maxDateStr)
			{
				retValue = true;

				fieldBox = document.getElementById(fieldBoxId);
				fieldObj = document.getElementById(fieldId);	
				dateStr = fieldObj.value;


				if(required == 0  && dateStr == '')
				{
					return true;
				}


				if(dateStr.charAt(2) != '/'  || dateStr.charAt(5) != '/' || dateStr.length != 10)
				{
					retValue = false;
				}	

				else	// format's okay; check max, min
				{
					currDays = parseInt(dateStr.substr(0,2),10) + parseInt(dateStr.substr(3,2),10)*30  + parseInt(dateStr.substr(6,4),10)*365;
					//alert(currDays);

					if(maxDateStr != '')
					{
						maxDays = parseInt(maxDateStr.substr(0,2),10) + parseInt(maxDateStr.substr(3,2),10)*30  + parseInt(maxDateStr.substr(6,4),10)*365;
						//alert(maxDays);
						if(currDays > maxDays)
							retValue = false;
					}

					if(minDateStr != '')
					{
						minDays = parseInt(minDateStr.substr(0,2),10) + parseInt(minDateStr.substr(3,2),10)*30  + parseInt(minDateStr.substr(6,4),10)*365;
						//alert(minDays);
						if(currDays < minDays)
							retValue = false;
					}
				}

				if(retValue == false)
				{
					fieldObj.setAttribute("class","mainFormError");
					fieldObj.setAttribute("className","mainFormError");
					fieldObj.focus();
					return false;
				}
			}
		//-->
		</SCRIPT>
		<!-- end validate -->




	</head>

	<body onLoad="collapseAll()">

	<div id="mainForm">




		<div id="formHeader">
				<h2 class="formInfo">Payment Type</h2>
				<p class="formInfo">Enter The Payment Type</p>
		</div>


		<BR/><!-- begin form -->
		<form method=post enctype=multipart/form-data action=processor.php onSubmit="return validatePage1();"><ul class=mainForm id="mainForm_1">

				<li class="mainForm" id="fieldBox_1">
					<label class="formFieldQuestion">Payment Type&nbsp;*&nbsp;<a class=info href=#><img src=imgs/tip_small.png border=0><span class=infobox>Enter The Payment Type</span></a></label><span><input class=mainForm type=radio name=field_1 id=field_1_option_1 value="Cash" /><label class=formFieldOption for="field_1_option_1">Cash</label><input class=mainForm type=radio name=field_1 id=field_1_option_2 value="Check" /><label class=formFieldOption for="field_1_option_2">Check</label><input class=mainForm type=radio name=field_1 id=field_1_option_3 value="Credit Card" /><label class=formFieldOption for="field_1_option_3">Credit Card</label><input class=mainForm type=radio name=field_1 id=field_1_option_4 value="Loan" /><label class=formFieldOption for="field_1_option_4">Loan</label></span></li>
		
		
		<!-- end of this page -->

		<!-- page validation -->
		<SCRIPT type=text/javascript>
		<!--
			function validatePage1()
			{
				retVal = true;
				if (validateField('field_1','fieldBox_1','radio',1) == false)
 retVal=false;

				if(retVal == false)
				{
					alert('Please correct the errors.  Fields marked with an asterisk (*) are required');
					return false;
				}
				return retVal;
			}
		//-->
		</SCRIPT>

		<!-- end page validaton -->



		<!-- next page buttons --><li class="mainForm">
					<input id="saveForm" class="mainForm" type="submit" value="Submit" />
				</li>

			</form>
			<!-- end of form -->
		<!-- close the display stuff for this page -->
		</ul></div><div id="footer"><p class="footer"><a class=footer href=http://phpformgen.sourceforge.net>Generated by phpFormGenerator</a></p></div>

	</body>
	</html>
Thanks for looking.

Last edited by NiteOwl; 07-09-2007 at 04:51 PM.. Reason: icon
NiteOwl is offline   Reply With Quote
Old 07-09-2007, 10:57 PM   PM User | #2
mcjwb
Regular Coder

 
Join Date: Jul 2007
Location: UK
Posts: 223
Thanks: 0
Thanked 14 Times in 14 Posts
mcjwb is an unknown quantity at this point
Add the following to the end of the series of if elses in the validateField function;

Code:
else if(fieldType == 'radio'){
     radioButtons = document.getElementsByName(fieldId);
     checked=false;
     for (i=(radioButtons.length-1); i>=0; i--)
     {
      if(radioButtons[i].checked){
       checked=true;
      }
     }
     return checked;
    }
mcjwb is offline   Reply With Quote
Old 07-09-2007, 11:10 PM   PM User | #3
NiteOwl
New Coder

 
Join Date: Aug 2006
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
NiteOwl is an unknown quantity at this point
Fantastic.... Thank You for your help.

Last edited by NiteOwl; 07-09-2007 at 11:12 PM..
NiteOwl is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:48 AM.


Advertisement
Log in to turn off these ads.