1 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 ; 25 26 31 32 public class ValidateTag extends BodyTagSupport { 33 34 String name; 36 37 String []reqdFields; 39 40 String method; 42 43 public void setName(String val) { 44 this.name = val; 45 } 46 47 public void setReqdFields(String val) { 49 StringTokenizer list = new StringTokenizer (val,","); 50 reqdFields = new String [list.countTokens()]; 51 int i = 0; 52 while (list.hasMoreTokens()) { 53 reqdFields[i++] = (String ) list.nextToken(); 54 } 55 } 56 57 public void setMethod(String name) { 58 this.method = name; 59 } 60 61 public int doStartTag() { 62 return EVAL_BODY_TAG; 63 } 64 65 71 72 public int doAfterBody() throws JspException { 73 74 try { 75 if (reqdFields.length == 0) return SKIP_BODY; 76 77 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 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 |