to change feild colour on validation(unfilled feilds change to red).

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gavy7210
    New Member
    • Aug 2009
    • 10

    to change feild colour on validation(unfilled feilds change to red).

    hello

    i am using struts 1.2,Eclipse Platform Version: 3.4.2,mySql 5.0.1,jdk 1.5..

    i have a login form(jsp) in which a user logs in,in case he doesnt enter his username and/or password an error is displayed using the <ul> tag and <html:errors> in the jsp. in the from class a vaidate method creates an actionerrors object adds all the errors to it and finally returns it.
    the ids are linked in messageresource s.properties and appropriate message is diaplayed.
    now i want to change the colour of the feild thats unfilled.. ie the username change its font to red in case it empty rather then a message.

    thanks in advance.

    code:

    index.jsp
    Code:
    <%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html"%>
    <%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean"%>
    <%@ taglib uri="/WEB-INF/tlds/struts-logic.tld" prefix="logic"%>
    
    
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    	pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-cache">
    
    <title>Welcome Page</title>
    <link href="example.css" rel="stylesheet" type="text/css" />
    </head>
    <body background="nexus.jpg">
    <div id="top">
    <table border="0">
    	<tr>
    		<td width="250"><img src="car.jpg" /></td>
    		<td width="250">
    		<img src="title.jpg" />
    		</td>
    		<td width="250">
    		<h2 align="right">
    		<logic:notPresent name="loginForm"	property="result" scope="session">
    		Welcome Please Sign In<br>
    		</logic:notPresent> 
    		
    		<logic:present name="loginForm" property="result" scope="session">
    		welcome <bean:write name="loginForm" property="uid" />!!
    		<html:link action="/logout">LogOut</html:link>
    			<html:link action="/viewinfo">Account Info</html:link>
    		</logic:present></h2>
    		</td>
    	</tr>
    </table>
    <hr width="100%">
    </div>
    
    
    <div id="leftnavigation">
    <h3>FEW LINKS</h3>
    <a href="newcar.jsp">New Car</a><br>
    <a href="oldcar.jsp">Old Car</a><br>
    <a href="Search.jsp">Search</a></div>
    
    <div id="content">
    <h1>Welcome to the car portal</h1>
    <p>This is the place where you can check out all the latest cars. <br>
    Not only this,you can also add your old cars for sale.<br>
    With our huge member base we are sure to get you a deal you cannot refuse. <br>
    <u>Register today to see the available cars at most affordable prices</u>.</p>
    
    <h2>New Launches</h2>
    <img src="car1.jpg" /></div>
    
    <div id="rightnavigation">
    <html:errors /> 
    <html:form action="/login">
    
    	<logic:present name="loginForm" property="flag" scope="session">
    	<font size="2" color="red">UserID/Password Does not exist.<br> New Users please register</font>
    	</logic:present>
    
    	<logic:notPresent name="loginForm" property="result" scope="session">
    
    
    		<h2>LOGIN HERE!!</h2>
    		<p>Enter your user id and password to log in</p>
    	USER NAME<font size="-1" color="red">*</font> <html:text property="uid" />
    		<br>
    	PASSWORD<font size="-1" color="red">*</font>  <html:password property="psswd" />
    		<br>
    		<html:submit value="LOG IN" />
    		<br>
    		<br>
    
    
    
    		<html:link forward="nregister">Register Here!!</html:link>
    
    	</logic:notPresent>
    
    </html:form>
    </div>
    </body>
    </html>
    LoginForm.java
    Code:
    package form;
    
    import javax.servlet.http.HttpServletRequest;
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionMessage;
    
    
    public class LoginForm extends ActionForm
    {
    	
    	 String Uid;
    	 String Psswd;
    	 String result;
    	 String flag;
    	 
    		
    	public String getFlag() {
    		return flag;
    	}
    	public void setFlag(String flag) {
    		this.flag = flag;
    	}
    	
    	public String getResult() {
    			return result;
    		}
    		public void setResult(String result) {
    			this.result = result;
    		}
    	 
    	
    	public String getUid() {
    		return Uid;
    	}
    	public void setUid(String uid) {
    		Uid = uid;
    	}
    	public String getPsswd() {
    		return Psswd;
    	}
    	public void setPsswd(String psswd) {
    		Psswd = psswd;
    	}
    	
    	public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)
    	{
    		ActionErrors actionErrors= new ActionErrors();
    		
    		if(getUid()=="")
    		{
    			actionErrors.add("Username",new ActionMessage("uid.required"));
    		}
    		
    		if(getPsswd()=="")
    		{
    			actionErrors.add("Password",new ActionMessage("psswd.required"));
    		}
    				
    		return actionErrors;
    	} 
    	
     public String test(String result)
     {    String act;
    	 if((getUid()=="") || (getPsswd()==""))
    		{
    		 act=null;
    		}
    		else
    		{
    		act="true";
    		}
    	 return act;}
    }
    MessageResource s.properties
    Code:
    # -- Standard errors --
    errors.header=<UL>
    errors.prefix=<LI><B><FONT COLOR="RED">
    errors.suffix=</FONT></B></LI>
    errors.footer=</UL>
    
    
    # -- Custom validation messages --
    
    uid.required=Username Required.
    uid.short=Username is too short.
    psswd.required=Password Required.
    psswd.short=Password is too short.
    rpsswd.required=Re Enter the password same as password.
    fname.required=First Name Required.
    lname.required=Last Name Required.
    email.required=Email Address Required.
    email.invalid=Invalid Email Address.
    email.incorrect=Incorrect Format for an Email Id.
    value.match=Username and password are same.
    value.notmatch=Password and retyped password don't match.
    digit.incorrect=Zip,Contact no and Date of birth must have only digits.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    So you have a CSS problem rather than a Java problem. Basically you want to change the font-color property based on whether the HTML control is filled or not after a failed submit.

    Comment

    • gavy7210
      New Member
      • Aug 2009
      • 10

      #3
      yes but through struts..
      using the action errors tag..
      i want to use the property of a bean to update the colour..
      something like that..
      to chnge colour isnt priority using struts to do it is

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        So use simple if-else logic to set the style or have two labels with different font and use simple if-else logic to decide which one to display.

        Comment

        • gavy7210
          New Member
          • Aug 2009
          • 10

          #5
          taking a que from what u said n using struts i solved the problem as below..

          <logic:messages Present>
          held the form when errors were there(and the feilds wre red) and a copy was kept in messagesNotPres ent tag.. normal feild display..

          thanks r035198x

          Comment

          • gavy7210
            New Member
            • Aug 2009
            • 10

            #6
            how do i close this thread!!

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by gavy7210
              how do i close this thread!!
              You can't, only moderators can do that; I'll close it for you.

              kind regards,

              Jos

              Comment

              Working...