User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 402,479 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,816 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting

Javascript, Form fields validation and submit

Join Date: Jan 2005
Posts: 18
Reputation: demo is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
demo demo is offline Offline
Newbie Poster

Re: Javascript, Form fields validation and submit

  #3  
Apr 26th, 2005
When you do the submit call the validation process and loop the form elements. Inside that loop create switch that contains all the form element types your form contains and then process each element based on it's type!

example...

<script>
<!-- //

	function good_add(eiv)
	{
		var teste = false;
		var strtest = new String(eiv);
		var index = strtest.indexOf("@");

		if (index > 0)
		{
			var eid = strtest.indexOf(".",index);

			if (eid > index+1 && strtest.length > eid+1)
			{
				teste = true;
			}

			return teste;
		}
	}

	function validate()
	{
		var e = '';

		for (var i = 0; i < document.test.elements.length; i++)
		{
    			var j = document.test.elements[i];

			switch (j.type)
			{
				case 'text':


				if (j.name == 'email')
				{
					if (j.value == '')
					{
						e += "Please enter a valid email\n";
					}
					else if (!good_add(j.value))
					{
						e += "Please enter a valid email\n";
					}
				}

				/* other 'input' type elements go here */

				break;

				case 'select-one':

				if (j.name == 'zip' && j.selectedIndex == 0)
				{
					e += "Please select a valid zip code\n";
				}

				/* other 'select' type elements go here */

				break;

				case 'checkbox':

				if (j.type == 'checkbox' && !j.checked)
				{
					e += "You must agree to our rules\n";
				}

				/* other 'checkbox' type elements go here */

				break;
			}
		}

		if ( e )
		{
			alert(e);

			return false;
		}
		else
		{
			/* submit would go here */

			// document.test.submit();
			// return true;

			alert('thanks for validating our form');

			return false;
		}
	}
// -->
</script>


<form name='test' action='process.php' onsubmit='return validate();' method='post'>
email <input type='text' name='email' value=''>
<br />
zip code <select name='zip'>
<option value=''>Select Zip Code
<option value='33510'>33510
<option value='02112'>02112</select>
<br />
<input type='checkbox' name='agree' value='1'> <small>do you agree with the rules</small>
<br />
<input type='submit' name='submit' value='Send'>
</form>


printf
Reply With Quote  
All times are GMT -4. The time now is 4:45 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC