KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > utility > basic > ValidateTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.taglibs.utility.basic;
17
18
19 import javax.servlet.*;
20 import javax.servlet.jsp.*;
21 import javax.servlet.jsp.tagext.*;
22
23 import java.io.*;
24 import java.util.StringTokenizer JavaDoc;
25
26 /**
27  * Add Javascript function to validate form input.
28  *
29  * Author Mandar Raje.
30  */

31
32 public class ValidateTag extends BodyTagSupport {
33     
34     // Name of the form.
35
String JavaDoc name;
36
37     // Required fields separeted by comma.
38
String JavaDoc []reqdFields;
39
40     // Name of the Javascript function to be generated.
41
String JavaDoc method;
42     
43     public void setName(String JavaDoc val) {
44         this.name = val;
45     }
46
47     // Parse the mandatory fields and store them into an array.
48
public void setReqdFields(String JavaDoc val) {
49     StringTokenizer JavaDoc list = new StringTokenizer JavaDoc(val,",");
50     reqdFields = new String JavaDoc [list.countTokens()];
51     int i = 0;
52     while (list.hasMoreTokens()) {
53         reqdFields[i++] = (String JavaDoc) list.nextToken();
54     }
55     }
56
57     public void setMethod(String JavaDoc name) {
58     this.method = name;
59     }
60     
61     public int doStartTag() {
62     return EVAL_BODY_TAG;
63     }
64
65     /**
66      * I am going to take an easy way out. Instead of parsing the HTML
67      * form tag and inserting onSubmit="methodName", I will shift the
68      * burden to the custom-tag user. This is not ideal, but hey,
69      * something is better than nothing.
70      **/

71      
72     public int doAfterBody() throws JspException {
73
74     try {
75         if (reqdFields.length == 0) return SKIP_BODY;
76         
77         // Javascript needs to be generated.
78
generateScript(bodyContent);
79         
80         bodyContent.writeOut(bodyContent.getEnclosingWriter());
81         return SKIP_BODY;
82         
83     } catch (IOException iox) {
84         throw new JspException(iox.getMessage());
85     }
86     }
87
88     /**
89      * Again I am going to take an easy way out.
90      * I am not generating the code per reqd. field.
91      * If you fail to provide a value for a reqd. field
92      * all it is going to say is "You have not filled
93      * out all the mandatory fields" without giving out
94      * the exact name for the field(s).
95      * This can be improved (after I am back from vacation).
96      **/

97     
98     public void generateScript(BodyContent out) throws IOException {
99
100     out.println("<SCRIPT LANGUAGE=\"JavaScript\">");
101     out.println("function " + this.method + "()");
102     out.println(" {");
103     out.println(" formObj = document." + this.name + ";");
104     out.print(" if (");
105     for(int i = 0; i < reqdFields.length; i++) {
106         if (i!= 0) out.println(" || ");
107         out.print("(formObj." + reqdFields[i] + ".value == \"\")");
108     }
109     out.println(")");
110     out.println(" {");
111     out.println(" alert(\"You have not filled out mandatory fields\");");
112     out.println(" return false;");
113     out.println(" }");
114     out.print(" else return true; ");
115     out.println(" }");
116     out.println("</SCRIPT>");
117     }
118 }
119
120     
121         
122     
123
Popular Tags