Solved

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

Posted on 2006-07-07
10
372 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ramareddyj
ID: 17063728
can i make the text color "RED" for a disabled text box?????
0
 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
getting Http 404 error in jsp forms 8 90
how to exclude a file using regex 5 123
How to set default webapp for host 6 47
Use tracing facilities in your browser steps 2 128
This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
A hard and fast method for reducing Active Directory Administrators members.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Suggested Courses

734 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