You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Nathan Coast <na...@codeczar.com> on 2005/01/17 05:31:37 UTC

validation syntax - decimal precision

Hi,

I have a price field that I want to validate as a float with these 
additional constraints 1-3 digits before the decimal, if the decimal 
exists, 1-2 digits after the decimal.

anyone know the regexp that describes this?

cheers
Nathan
-- 
Nathan Coast
Managing Director
Codeczar Ltd
mob : (852) 9049 5581
tel : (852) 2834 8733
fax : (852) 2834 8755
web : http://www.codeczar.com

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [OT] validation syntax - decimal precision

Posted by Curtis Taylor <cu...@yahoo.com>.
Hi Nathan,

Two suggestions:

1.) Mastering Regular Expressions by J.E.E. Friedel 
(http://www.amazon.com/exec/obidos/ASIN/0596002890/qid=1105937543/sr=2-1/ref=pd_ka_b_2_1/002-6332011-0856023)

2.) Kodos (http://kodos.sourceforge.net/home.html)

Good luck,

Curtis

Nathan Coast wrote:
> Hi,
> 
> I have a price field that I want to validate as a float with these 
> additional constraints 1-3 digits before the decimal, if the decimal 
> exists, 1-2 digits after the decimal.
> 
> anyone know the regexp that describes this?
> 
> cheers
> Nathan


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: validation syntax - decimal precision

Posted by Kishore Senji <ks...@gmail.com>.
how about "^\d{1,3}(\.\d{1,2})?$"

Here is the testcase

import org.apache.commons.validator.GenericValidator;

public class TestFloat{


  public static void main(String[] args) throws Exception {

    String[] f = new String[]{"123.23", "123", "1234.23", "1234.234",
"12345.2342", "11234.123.23", "123.3", "1234.23",};

    for(int i=0; i<f.length; i++){
		System.out.println(f[i]+" "+testFloat(f[i]));
	}

  }


  public static boolean testFloat(String f){

	  final String PATTERN = "^\\d{1,3}(\\.\\d{1,2})?$";

	  return GenericValidator.matchRegexp(f, PATTERN);
  }
}


On Mon, 17 Jan 2005 12:31:37 +0800, Nathan Coast <na...@codeczar.com> wrote:
> Hi,
> 
> I have a price field that I want to validate as a float with these
> additional constraints 1-3 digits before the decimal, if the decimal
> exists, 1-2 digits after the decimal.
> 
> anyone know the regexp that describes this?
> 
> cheers
> Nathan
> --
> Nathan Coast
> Managing Director
> Codeczar Ltd
> mob : (852) 9049 5581
> tel : (852) 2834 8733
> fax : (852) 2834 8755
> web : http://www.codeczar.com
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org