Solved

Javascript to catch bad form before submit

Posted on 2008-10-07
3
428 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
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
you are welcome
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now