Solved

how to write an error message on a jsp by calling a js function from jsp

Posted on 2006-07-07
10
361 Views
Last Modified: 2010-04-01
hi ,
i am facing the problem of showing an error message on a jsp page once the user clicks a link.

The error message is to be shown on the same page where the link is shown.

the code is as below

(<html:link href="#" onclick="document.write(indexEnableInMatch('pmformforindexing'))">
                                                                  <bean:message bundle="POIMatches" key="pmr.details.link" />
                                                                       </html:link>)


not abel to understand what code should i write on the the event onClick?

kindly help me

regards,
jatinder

0
Comment
Question by:ramareddyj
  • 2
  • 2
  • 2
  • +2
10 Comments
 
LVL 5

Expert Comment

by:jpolin1
ID: 17058877
Looks like you are using struts, why not use its built in error checking?

If you want it to display an error message w/o a page refresh(another request-responce cycle),
then you need some DHTML, like swapping layers in and out via javascript.

Let me know which way you are trying to go.
0
 

Author Comment

by:ramareddyj
ID: 17058987
i am using struts framework. and i have decide to show the error message in a textbox which will be disabled all the time

what i am trying to do now is on execution of following line
onclick="indexEnableInMatch(<%='pmformforindexing'%>)">

my indexEnableInMatch() will be called and inside this function i am going to set the value of a text box to my error message

function indexEnableInMatch(FormName)
{
      alert(FormName);
      FormName.hiddenLabel.value="my error emssage ";

}

now i am facing problem in passing  the parameter "pmformforindexing" which is nothing but my action form.

any way to resolve this??

regards,
jatinder
0
 
LVL 5

Accepted Solution

by:
jpolin1 earned 125 total points
ID: 17059194
Ideally you should use struts built in form-validation.
Are you familar with this at all? There are a couple of different ways to go with it.
It will be easier to setup and easier to maintain down the road.
If the data you want to validate is already on form beans associated with your actions, then it wouldn't be too much work to get the wiring together to make this happen.

Basically all you do is override the validate method in your form beans, perform your error check there.
If you have errors you create a collection of them and return them. Then all you have to do is create a resource bundle for the actual messages and put 1 new tag on your jsp.

The other good thing is that the validation is handled on the server side, rather than client side like you are trying to do with javascript. If you do your validation on the server side, you are guarenteed to be getting good data, javascript can always be disabled or hacked around.

The latest way of doing validation within struts uses the validation framework. There is a ton of stuff out there on this. I haven't used this approach yet.
0
 

Author Comment

by:ramareddyj
ID: 17063728
can i make the text color "RED" for a disabled text box?????
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Assisted Solution

by:Meritor
Meritor earned 125 total points
ID: 17064595
If you are using IE then its hard to set color for disabled text box but in Mozilla/Netscape you can write the following code:
input[disabled] {color:red!important;}

As in:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title> New Document </title>
<style type="text/css">
input {
color: green;
}
input[disabled] {color:red!important;}
</style>

</head>

<body>
<form>
<input type="text" disabled value="disabled"></input><br>
<input type="text" value="normal"></input>
</form>
</body>
</html>
0
 
LVL 5

Assisted Solution

by:muktajindal
muktajindal earned 125 total points
ID: 17071822
You should use server-side validation provision by Struts to solve this. Are you aware of Struts Form validation using "validate" method in your Form classes? Just try that and pass errors from there to your jsp files and display there.

For textbox color change, you can define two separate classes for active and disabled textboxes in your CSS file. Create textboxes with the active class. Whenever you want to disable a textbox, just change the class associated with that textbox. You should get the desired results.
0
 
LVL 10

Assisted Solution

by:jaggernat
jaggernat earned 125 total points
ID: 17349532
You should use the "Struts validation framework". Its very simple to use and very powerful.

Struts validation framework comprises primarily of two files, validation.xml and validation-rules.xml(provided by struts framework).

lets say you want to validate a field "xyz" in your jsp,
In your validation.xml file , you have to write

<form name="nameoftheformbean">
<field property="xyz" depends="required">
<arg0 key="showerrormessage"/>
</field>
</form>


<field property="xyz" depends="required">  //reuired is a rule defined in validation-rules.xml file which means that the field "xyz" cannot be null.
<arg0 key="showerrormessage"/>  //showerrormessage is defined in ApplicationResources.properties file like
showerrormessage=Please enter a value in the field xyz

and thats it . Its that simple.


In your struts-config , you have to put
validate="true" and
input="NameOfJSP"

Basically this means perform the validation and if there are any errors show them on NameOfJSP.jsp

Like "required" is a rule, there are other rules.

Take a look at this excellent article from Orielley

http://www.onjava.com/pub/a/onjava/2002/12/11/jakartastruts.html

hope that helps
J
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17349555
>>>>>   input="NameOfJSP"

I meant    input="NameOfJSP.jsp"



0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Find out what Office 365 Transport Rules are, how they work and their limitations managing Office 365 signatures.
We have come a long way with backup and data protection — from backing up to floppies, external drives, CDs, Blu-ray, flash drives, SSD drives, and now to the cloud.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

706 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

19 Experts available now in Live!

Get 1:1 Help Now