Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in Struts Validation

 
Hariharan.S
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to use Struts Validation. But I didn�t get , Please help me to solve it.
I am getting error message as validateLoginForm is not defined.

Here are the files I am using

Login.jsp

<html:form action="/login" method="post" onsubmit="validateLoginForm();">
.
.
.
.
 <html:text property="userName" styleClass="txtstyle" size="20" maxlength="15"/>

 <html assword property="password" styleClass="txtstyle" size="20" maxlength="15"/>

<html:javascript formName="loginForm"/>

</html:form>

</body>
</html:html>


struts-config.xml

<form-bean name="loginForm" type="com.newhorizon.vrm.web.form.common.LoginForm" />


<action
attribute="loginForm"
input="/jsp/common/login.jsp"
name="loginForm"
path="/login"
validate=�true�
type="com.newhorizon.vrm.web.action.common.LoginAction">
<forward name="loginfailure" path="/jsp/common/login.jsp" />
<forward name="loginsuccess" path="/jsp/common/home.jsp" />
</action>

<!-- Message Resources -->
<message-resources parameter="com.newhorizon.vrm.web.ApplicationResources" />

<!-- Validator plugin -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>



Validation.xml

<!-- Login form Validation-->
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//
DTD Commons Validator Rules Configuration 1.2//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_2_0.dtd">

<form-validation>


<formset>
<form name="loginForm">
<field property="userName" depends="required"> <arg0 key="userName"/>
</field>

<field property="password" depends="required,mask">
<arg0 key="password"/>
<var>
<var-name>mask</var-name>
<var-value>^[0-9a-zA-Z]*$</var-value>
</var>
</field>
</form>
</formset>

</form-validation>


LoginForm.java

import org.apache.struts.validator.*;
public class LoginForm extends ValidatorForm {
private String userName = null;
private String password = null;

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}



}

LoginAction.java

public class LoginAction extends Action {

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)throws IOException, ServletException
{
System.out.println("insideerrors : " );
LoginForm loginform =(LoginForm) form;

String username = loginform.getUserName();
String password = loginform.getPassword();

if(username.length()!=0)
{
String res =username+"<br>"+password;
javax.servlet.http.HttpSession session = request.getSession();
session.setAttribute("result",res);
return mapping.findForward("loginsuccess");
}
else
{
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("errors.global"));
saveErrors(request,errors);
System.out.println("errors : " + errors);
return(mapping.findForward("loginfailure"));
}
}
}


ApplicationResources.properties

errors.nulluserName='enter your ID
errors.nullpassword=enter your PASSWORD
errors.global=DOES NOT MATCH
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your onsubmit should be

onsubmit="return validateLoginForm(this);"

You use return so that if the form is not valid, the form does not submit (returns false). You use this as an argument to the validateLoginForm function because it takes the form itself as a parameter.
[ May 18, 2006: Message edited by: Gregg Bolinger ]
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, your display name does not completely comply with our Naming Policy. Initials are ok for the first half (first name) of your name, but not the second half (last name).

Please see what you can do about that.

Thanks.
 
Hariharan.S
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

yeah now i am getting. But i got alert as ' is required' . I want to display like 'UserName is required' 'Password is required'. My ApplicationResources.properties file is here..


errors.invalid={0} is invalid.

errors.maxlength=pincode can not be greater than 6 characters.

errors.minlength=password can not be less than 6 characters.

errors.range={0} is not in the range {1} through {2}.

errors.required={0} is required.

errors.byte={0} must be an byte.

errors.date={0} is not a date.

errors.double={0} must be an double.

errors.float={0} must be an float.

errors.integer={0} must be an integer.

errors.long={0} must be an long.

errors.short={0} must be an short.

errors.creditcard={0} is not a valid credit card number.

errors.email={0} is an invalid e-mail address.

errors.nulluserName=ID
errors.nullpassword=PASSWORD
 
Hariharan.S
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i got it by assigning a string to the key in AR.properties.

like : userName=UserName

Is this approach good or any other way.
 
Hariharan.S
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am getting alert msg for 'password is required'. But i am not getting alert msg for 'password minlength'. But if i have specified <html:errors/> in the jsp page, i am getting proper error message in the form of text. Why this problem occurs?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic