Solved

Javascript to catch bad form before submit

Posted on 2008-10-07
3
446 Views
Last Modified: 2012-06-21
What code would I need for a javascript function checkform();
That would make sure:
"name" and "color" are not empty ie:  obj.value!=""

If one of them IS empty, then show the div named "warning" and NOT submit the form

However if, they are both populated, then SUBMIT the form


Thanks!
<form id="form1" name="form1" method="post" action="go.php">
 
  <label>Your Name<input type="text" name="name" id="name" /></label>
 
  Your Color:
  <label><input type="radio" name="color" id="color" value="red"  />Red</label>
  <label><input type="radio" name="color" id="color" value="green"  />Green</label>
  <label><input type="radio" name="color" id="color" value="blue"  />Blue</label>
 
  <div id="warning" style="display:none"> Please Complete The Form before submitting</div>
 
    <input type="submit" name="Submit" value="Submit" onclick="checkform(this); return false;" />
 
</form>

Open in new window

0
Comment
Question by:dban00b
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 22664207
copy and paste this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title></title>
<script type="text/javascript">
function checkForm(f)
{
	var e="";
	if( f.name.value=="" )
		e+="\nPlease enter your name";
	var c=false;
	for( var i=0; i < f.color.length; ++i)
		if( f.color[i].checked ) c=true;
	if(!c)
		e+="\nPlease Choose a color";
	if( e )
		alert(e)
return e=="";
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="go.php" onsubmit="return checkForm(this)">
 
  <label>Your Name<input type="text" name="name" id="name" /></label>
 
  Your Color:
  <label><input type="radio" name="color" id="color" value="red"  />Red</label>
  <label><input type="radio" name="color" id="color" value="green"  />Green</label>
  <label><input type="radio" name="color" id="color" value="blue"  />Blue</label>
 
  <div id="warning" style="display:none"> Please Complete The Form before submitting</div>
 
    <input type="submit" name="Submit" value="Submit" />
 
</form> 
</body>
</html>

Open in new window

0
 
LVL 1

Author Comment

by:dban00b
ID: 22664511
Not quite right, but pretty darn close, got me what I need, thanks!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title></title>
<script type="text/javascript">
function checkForm()
{
		var f=document.getElementById('form1');
        var e="";
        if( f.name.value=="" )
                e+="\nPlease enter your name";
        var c=false;
        for( var i=0; i < f.color.length; ++i)
                if( f.color[i].checked ) c=true;
        if(!c)
                e+="\nPlease Choose a color";
        if(e)
				document.getElementById('warning').style.display='';
		else
			 f.submit();
return e=="";
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="go.php">
 
  <label>Your Name<input type="text" name="name" id="name" /></label>
 
  Your Color:
  <label><input type="radio" name="color" id="color" value="red"  />Red</label>
  <label><input type="radio" name="color" id="color" value="green"  />Green</label>
  <label><input type="radio" name="color" id="color" value="blue"  />Blue</label>
 
  <div id="warning" name="warning" style="display:none;"> Please Complete The Form before submitting</div>
 
    <input type="button" name="Submit" value="Submit" onClick="checkForm(); return false;" />
 
</form> 
</body>
</html>

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 22668817
you are welcome
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question