From: "Givler, Eric" <[EMAIL PROTECTED]>
Reply-To: "Struts Users Mailing List" <user@struts.apache.org>
To: "Struts Users Mailing List" <user@struts.apache.org>
Subject: RE: validate integer with a comma thousands seperator using
Validator
Date: Thu, 3 Aug 2006 11:45:28 -0400
You could create another validator, and inside that perform a stripping of
non-numeric characters (stripCommas), then
execute code similar to the existing "long range" validator like this:
/**
* Determines if a formatted numeric value is between a range as
defined by
* the user-supplied variables: min and max
* <p>
* Note: The only formattting performed is stripping off any commas.
If the
* resulting number does not convert to a Long, then validation will
fail as
* well.
* </p>
* <p>
* Validation succeeds if:
* <ol>
* <li>The value in question is null.</li>
* <li>The parsed field value is within the user-supplied
range.</li>
* </ol>
* </p>
* @return
* @param application
* @param request
* @param errors
* @param field
* @param va
* @param bean
*/
public static boolean validateFormattedLongRange(
Object bean,
ValidatorAction va,
Field field,
ActionMessages errors,
HttpServletRequest request,
ServletContext application)
{
System.out.println("*** validateFormattedLongRange (" +
field.getProperty() + ") - START");
boolean isValid = true;
String value = null;
value = ValidationUtils.evaluateBean(bean, field);
if (!GenericValidator.isBlankOrNull(value))
{
try
{
long minVal = Long.parseLong(field.getVarValue("min"));
long maxVal = Long.parseLong(field.getVarValue("max"));
// We don't want to do this unless we use map.put to show
tampered value
// value = value.replaceAll("[^-0123456789.]", "");
value = value.replaceAll("[,]", "");
long lngValue = Long.parseLong(value);
if ((lngValue > maxVal) || (lngValue < minVal))
{
errors.add(field.getKey(),
Resources.getActionMessage(request, va, field));
isValid = false;
}
}
catch (NumberFormatException nfex)
{
errors.add(field.getKey(),
Resources.getActionMessage(request, va, field));
isValid = false;
}
}
System.out.println("*** validateFormattedLongRange (" +
field.getProperty() + ") - END, returning " + isValid);
return isValid;
}
Define this validator in validator-rules.xml:
<validator name="fmtLongRange"
classname="view.struts.validator.StrutsValidationExtensions"
method="validateFormattedLongRange"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
javax.servlet.http.HttpServletRequest,
javax.servlet.ServletContext"
msg="errors.fmtLongRange">
</validator>
Add a rule for a field:
<field property="water_usage" page="2" depends="fmtLongRange">
<arg0 name="required" key="Page_2.water_usage"/>
<arg0 name="fmtLongRange" key="Page_2.water_usage" />
<arg1 name="fmtLongRange" key="${var:min}" resource="false"/>
<arg2 name="fmtLongRange" key="999,999,999" resource="false"/>
<var><var-name>min</var-name><var-value>0</var-value></var>
<var><var-name>max</var-name><var-value>999999999</var-value></var>
</field>
Define the message in the resource file (errors.fmtLongRange):
errors.fmtLongRange={0} must be a valid whole number between {1} and {2}.
-----Original Message-----
From: fea jabi [mailto:[EMAIL PROTECTED]
Sent: Thursday, August 03, 2006 10:01 AM
To: user@struts.apache.org
Subject: RE: validate integer with a comma thousands seperator using
Validator
can someone help me with this please?
>From: "fea jabi" <[EMAIL PROTECTED]>
>Reply-To: "Struts Users Mailing List" <user@struts.apache.org>
>To: user@struts.apache.org
>Subject: validate integer with a comma thousands seperator using
Validator
>Date: Wed, 02 Aug 2006 10:09:19 -0400
>
>Using struts validator.
>
>have to validate the user entered value.
>
>The value entered should be a positive integer with a comma thousands
>seperator. the number need not be in thousands.
>
>I have as below to check for positive integer without comma seperator.
but
>not sure how to validate if the user entered value with a comma
seperator?
>
>i.e value like 25,349 // how to validate this?
>
><field property="hrs" depends="integer,validwhen">
> <msg name="integer" key="errors.notvalid"/>
> <msg name="validwhen" key="errors.notvalid"/>
> <var>
> <var-name>test</var-name>
> <var-value>(*this* >= 0)</var-value>
> </var>
> </field>
>
>how to validate the user entered value with a comma seperator? Thanks.
>
>_________________________________________________________________
>Don't just search. Find. Check out the new MSN Search!
>http://search.msn.click-url.com/go/onm00200636ave/direct/01/
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]
>
_________________________________________________________________
Don't just search. Find. Check out the new MSN Search!
http://search.msn.click-url.com/go/onm00200636ave/direct/01/
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]