Solved

Display Error Message on HTML form using Javascript

Posted on 2009-04-13
3
8,380 Views
Last Modified: 2012-05-06
Hi Experts,

I copied the sample code below from a related question the knowledgebase....

Is there any way, I can get the error message from the javascript to display in the html form itself, below the first name, last name and phone number? If so, can you provide the modified logic to the sample below?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript"><!--
function validate(f)
{
	var err={};
	
	if( f.fname.value=="")
	{
		err.message="Missing First Name";
		err.field=f.fname;
	}
	else if( f.lname.value=="")
	{
		err.message="Missing Last Name";
		err.field=f.lname;
		
	}
	else if( f.phone.value=="")
	{
		err.message="Missing Phone Number";
		err.field=f.phone;
	}
	if(err.message)
	{
		alert(err.message);
		err.field.focus();
		return false;
	}
return true;
}
//--></script>
</head>
<body>
<form action="https://www.someserver.com/file?encoding=UTF-8" method="POST" onsubmit="return validate(this);">
    <input id="fname" name="fname" type="text" value="" />
    <input id="lname" name="lname" type="text" value="" />
    <input id="phone" name="phone" type="text" value="" />
    <input id="submit" name="Submit" type="submit" value="Submit" />
</form> 
</body>
</html>

Open in new window

0
Comment
Question by:bluegreen288
[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 19

Accepted Solution

by:
LordOfPorts earned 100 total points
ID: 24131856
Yes, simply add e.g. a DIV below the form and assign to it the error message:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript"><!--
function validate(f)
{
        var err={};
        
        if( f.fname.value=="")
        {
                err.message="Missing First Name";
                err.field=f.fname;
        }
        else if( f.lname.value=="")
        {
                err.message="Missing Last Name";
                err.field=f.lname;
                
        }
        else if( f.phone.value=="")
        {
                err.message="Missing Phone Number";
                err.field=f.phone;
        }
        if(err.message)
        {
                document.getElementById('divError').innerHTML = err.message;
                err.field.focus();
                return false;
        }
return true;
}
//--></script>
</head>
<body>
<form action="https://www.someserver.com/file?encoding=UTF-8" method="POST" onsubmit="return validate(this);">
    <input id="fname" name="fname" type="text" value="" />
    <input id="lname" name="lname" type="text" value="" />
    <input id="phone" name="phone" type="text" value="" />
    <input id="submit" name="Submit" type="submit" value="Submit" />
</form>
<div id="divError"></div>
</body>
</html>

Open in new window

0
 
LVL 41

Assisted Solution

by:HonorGod
HonorGod earned 25 total points
ID: 24132023
And choose a CSS class name that makes the messages stand out.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24410720
Thanks for the assist and the points.

Good luck & have a great day
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

737 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