org.apache.commons.validator.routines

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache commons validator 1.3.1 src » org.apache.commons.validator.routines 
org.apache.commons.validator.routines
Package Documentation for org.apache.commons.validator.routines Package

This package contains independant validation routines.

Table of Contents

1. Overview

Commons Validator serves two purposes:

  • To provide standard, independant validation routines/functions.
  • To provide a mini framework for Validation.

This package has been created, since version 1.3.0, in an attempt to clearly separate these two concerns and is the location for the standard, independant validation routines/functions in Commons Validator.

The contents of this package have no dependencies on the framework aspect of Commons Validator and can be used on their own.

2. Date and Time Validators

2.1 Overview

The date and time validators either validate according to a specified format or use a standard format for a specified Locale.

2.2 Validating a Date Value

You can either use one of the isValid() methods to just determine if a date is valid, or use one of the validate() methods to validate a date and convert it to a java.util.Date...

      // Get the Date validator
      DateValidator validator = DateValidator.getInstance();

      // Validate/Convert the date
      Date fooDate = validator.validate(fooString, "dd/MM/yyyy");
      if (fooDate == null) {
          // error...not a valid date
          return;
      }

The following methods are provided to validate a date/time (return a boolean result):

  • isValid(value)
  • isValid(value, pattern)
  • isValid(value, Locale)
  • isValid(value, pattern, Locale)

The following methods are provided to validate a date/time and convert it to either a java.util.Date or java.util.Calendar:

  • validate(value)
  • validate(value, pattern)
  • validate(value, Locale)
  • validate(value, pattern, Locale)

2.3 Formatting

Formatting and validating are two sides of the same coin. Typically input values which are converted from Strings according to a specified format also have to be rendered for output in the same format. These validators provide the mechanism for formatting from date/time objects to Strings. The following methods are provided to format date/time values as Strings:

  • format(date/calendar)
  • format(date/calendar, pattern)
  • format(date/calendar, Locale)
  • format(date/calendar, pattern, Locale)

2.4 Time Zones

If the date being parsed relates to a different time zone than the system default, you can specify the TimeZone to use when validating/converting:

      // Get the GMT time zone
      TimeZone GMT = TimeZone.getInstance("GMT");

      // Validate/Convert the date using GMT
      Date fooDate = validator.validate(fooString, "dd/MM/yyyy", GMT);

The followng Time Zone flavours of the Validation/Conversion methods are provided:

  • validate(value, TimeZone)
  • validate(value, pattern, TimeZone)
  • validate(value, Locale, TimeZone)
  • validate(value, pattern, Locale, TimeZone)

2.5 Comparing Dates and Times

As well as validating that a value is a valid date or time, these validators also provide date comparison functions. The DateValidator and CalendarValidator provide functions for comparing years, quarters, months, weeks and dates and the TimeValidator provides functions for comparing hours, minutes, seconds and milliseconds. For example, to check that a date is in the current month, you could use the compareMonths() method, which compares the year and month components of a date:

      // Check if the date is in the current month 
      int compare = validator.compareMonths(fooDate, new Date(), null); 
      if (compare == 0) { 
          // do current month processing
          return;
      }

      // Check if the date is in the previous quarter
      compare = validator.compareQuarters(fooDate, new Date(), null);
      if (compare < 0) {
          // do previous quarter processing
          return;
      }

      // Check if the date is in the next year
      compare = validator.compareYears(fooDate, new Date(), null);
      if (compare > 0) {
          // do next year processing
          return;
      }

3 Numeric Validators

3.1 Overview

The numeric validators either validate according to a specified format or use a standard format for a specified Locale or use a custom format for a specified Locale.

3.2 Validating a Numeric Value

You can either use one of the isValid() methods to just determine if a number is valid, or use one of the validate() methods to validate a number and convert it to an appropriate type.

The following example validates an integer against a custom pattern for the German locale. Please note the format is specified using the standard symbols for java.text.DecimalFormat so although the decimal separator is indicated as a period (".") in the format, the validator will check using the German decimal separator - which is a comma (",").

      // Get the Integer validator
      IntegerValidator validator = IntegerValidator.getInstance();

      // Validate/Convert the number
      Integer fooInteger = validator.validate(fooString, "#,##0.00", Locale.GERMAN);
      if (fooInteger == null) {
          // error...not a valid Integer
          return;
      }

The following methods are provided to validate a number (return a boolean result):

  • isValid(value)
  • isValid(value, pattern)
  • isValid(value, Locale)
  • isValid(value, pattern, Locale)

The following methods are provided to validate a number and convert it one of the java.lang.Number implementations:

  • validate(value)
  • validate(value, pattern)
  • validate(value, Locale)
  • validate(value, pattern, Locale)

3.3 Formatting

Formatting and validating are two sides of the same coin. Typically input values which are converted from Strings according to a specified format also have to be rendered for output in the same format. These validators provide the mechanism for formatting from numeric objects to Strings. The following methods are provided to format numeric values as Strings:

  • format(number)
  • format(number, pattern)
  • format(number, Locale)
  • format(number, pattern, Locale)

3.4 Comparing Numbers

As well as validating that a value is a valid number, these validators also provide functions for validating the minimum, maximum and range of a value.

      // Check the number is between 25 and 75
      if (validator.isInRange(fooInteger, 25, 75) {
          // valid...in the specified range
          return;
      }

3.5 Currency Validation

A default Currency Validator implementation is provided, although all the numeric validators support currency validation. The default implementation converts currency amounts to a java.math.BigDecimal and additionally it provides lenient currency symbol validation. That is, currency amounts are valid with or without the currency symbol.

      BigDecimalValidator validator = CurrencyValidator.getInstance();

      BigDecimal fooAmount = validator.validate("$12,500.00", Locale.US);
      if (fooAmount == null) {
          // error...not a valid currency amount
          return;
      }

      // Check the amount is a minimum of $1,000
      if (validator.minValue(fooAmount, 1000) {
          // valid...in the specified range
          return;
      }

If, for example, you want to use the Integer Validator to validate a currency, then you can simply create a new instance with the appropriate format style. Note that the other validators do not support the lenient currency symbol validation.

      IntegerValidator validator = 
          new IntegerValidator(true, IntegerValidator.CURRENCY_FORMAT);

      String pattern = "#,###" + '\u00A4' + '\u00A4';  // Use international symbol

      Integer fooAmount = validator.validate("10.100EUR", pattern, Locale.GERMAN);
      if (fooAmount == null) {
          // error...not a valid currency amount
          return;
      }

3.6 Percent Validation

A default Percent Validator implementation is provided, although the Float, Double and BigDecimal validators also support percent validation. The default implementation converts percent amounts to a java.math.BigDecimal and additionally it provides lenient percent symbol validation. That is, percent amounts are valid with or without the percent symbol.

      BigDecimalValidator validator = PercentValidator.getInstance();

      BigDecimal fooPercent = validator.validate("20%", Locale.US);
      if (fooPercent == null) {
          // error...not a valid percent
          return;
      }

      // Check the percent is between 10% and 90%
      if (validator.isInRange(fooPercent, 0.1, 0.9) {
          // valid...in the specified range
          return;
      }

If, for example, you want to use the Float Validator to validate a percent, then you can simply create a new instance with the appropriate format style. Note that the other validators do not support the lenient percent symbol validation.

      FloatValidator validator = 
          new FloatValidator(true, FloatValidator.PERCENT_FORMAT);

      Float fooPercent = validator.validate("20%", "###%");
      if (fooPercent == null) {
          // error...not a valid percent
          return;
      }

Note: in theory the other numeric validators besides Float, Double and BigDecimal (i.e. Byte, Short, Integer, Long and BigInteger) also support percent validation. However, since they don't allow fractions they will only work with percentages greater than 100%.

Java Source File NameTypeComment
AbstractCalendarValidator.javaClass
AbstractFormatValidator.javaClass
AbstractNumberValidator.javaClass
BaseCalendarValidatorTest.javaClass Base Calendar Test Case.
BaseNumberValidatorTest.javaClass Base Number Test Case.
BigDecimalValidator.javaClass

BigDecimal Validation and Conversion routines (java.math.BigDecimal).

This validator provides a number of methods for validating/converting a String value to a BigDecimal using java.text.NumberFormat to parse either:

  • using the default format for the default Locale
  • using a specified pattern with the default Locale
  • using the default format for a specified Locale
  • using a specified pattern with a specified Locale

Use one of the isValid() methods to just validate or one of the validate() methods to validate and receive a converted BigDecimal value.

Fraction/decimal values are automatically trimmed to the appropriate length.

Once a value has been sucessfully converted the following methods can be used to perform minimum, maximum and range checks:

  • minValue() checks whether the value is greater than or equal to a specified minimum.
  • maxValue() checks whether the value is less than or equal to a specified maximum.
  • isInRange() checks whether the value is within a specified range of values.

So that the same mechanism used for parsing an input value for validation can be used to format output, corresponding format() methods are also provided.

BigDecimalValidatorTest.javaClass Test Case for BigDecimalValidator.
BigIntegerValidator.javaClass

BigInteger Validation and Conversion routines (java.math.BigInteger).

This validator provides a number of methods for validating/converting a String value to a BigInteger using java.text.NumberFormat to parse either:

  • using the default format for the default Locale
  • using a specified pattern with the default Locale
  • using the default format for a specified Locale
  • using a specified pattern with a specified Locale

Use one of the isValid() methods to just validate or one of the validate() methods to validate and receive a converted BigInteger value.

Once a value has been sucessfully converted the following methods can be used to perform minimum, maximum and range checks:

  • minValue() checks whether the value is greater than or equal to a specified minimum.
  • maxValue() checks whether the value is less than or equal to a specified maximum.
  • isInRange() checks whether the value is within a specified range of values.

So that the same mechanism used for parsing an input value for validation can be used to format output, corresponding format() methods are also provided.

BigIntegerValidatorTest.javaClass Test Case for BigIntegerValidator.
ByteValidator.javaClass

Byte Validation and Conversion routines (java.lang.Byte).

This validator provides a number of methods for validating/converting a String value to a Byte using java.text.NumberFormat to parse either:

  • using the default format for the default Locale
  • using a specified pattern with the default Locale
  • using the default format for a specified Locale
  • using a specified pattern with a specified Locale

Use one of the isValid() methods to just validate or one of the validate() methods to validate and receive a converted Byte value.

Once a value has been sucessfully converted the following methods can be used to perform minimum, maximum and range checks:

  • minValue() checks whether the value is greater than or equal to a specified minimum.
  • maxValue() checks whether the value is less than or equal to a specified maximum.
  • isInRange() checks whether the value is within a specified range of values.

So that the same mechanism used for parsing an input value for validation can be used to format output, corresponding format() methods are also provided.

ByteValidatorTest.javaClass Test Case for ByteValidator.
CalendarValidator.javaClass

Calendar Validation and Conversion routines (java.util.Calendar).

This validator provides a number of methods for validating/converting a String date value to a java.util.Calendar using java.text.DateFormat to parse either:

  • using the default format for the default Locale
  • using a specified pattern with the default Locale
  • using the default format for a specified Locale
  • using a specified pattern with a specified Locale

For each of the above mechanisms, conversion method (i.e the validate methods) implementations are provided which either use the default TimeZone or allow the TimeZone to be specified.

Use one of the isValid() methods to just validate or one of the validate() methods to validate and receive a converted Calendar value.

Implementations of the validate() method are provided to create Calendar objects for different time zones if the system default is not appropriate.

Alternatively the CalendarValidator's adjustToTimeZone() method can be used to adjust the TimeZone of the Calendar object afterwards.

Once a value has been sucessfully converted the following methods can be used to perform various date comparison checks:

  • compareDates() compares the day, month and year of two calendars, returing 0, -1 or +1 indicating whether the first date is equal, before or after the second.
  • compareWeeks() compares the week and year of two calendars, returing 0, -1 or +1 indicating whether the first week is equal, before or after the second.
  • compareMonths() compares the month and year of two calendars, returing 0, -1 or +1 indicating whether the first month is equal, before or after the second.
  • compareQuarters() compares the quarter and year of two calendars, returing 0, -1 or +1 indicating whether the first quarter is equal, before or after the second.
  • compareYears() compares the year of two calendars, returing 0, -1 or +1 indicating whether the first year is equal, before or after the second.

So that the same mechanism used for parsing an input value for validation can be used to format output, corresponding format() methods are also provided.

CalendarValidatorTest.javaClass Test Case for CalendarValidator.
CurrencyValidator.javaClass

Currency Validation and Conversion routines (java.math.BigDecimal).

This is one implementation of a currency validator that has the following features:

  • It is lenient about the the presence of the currency symbol
  • It converts the currency to a java.math.BigDecimal

However any of the number validators can be used for currency validation. For example, if you wanted a currency validator that converts to a java.lang.Integer then you can simply instantiate an IntegerValidator with the appropriate format type:

...

CurrencyValidatorTest.javaClass Test Case for CurrencyValidator.
DateValidator.javaClass

Date Validation and Conversion routines (java.util.Date).

This validator provides a number of methods for validating/converting a String date value to a java.util.Date using java.text.DateFormat to parse either:

  • using the default format for the default Locale
  • using a specified pattern with the default Locale
  • using the default format for a specified Locale
  • using a specified pattern with a specified Locale

For each of the above mechanisms, conversion method (i.e the validate methods) implementations are provided which either use the default TimeZone or allow the TimeZone to be specified.

Use one of the isValid() methods to just validate or one of the validate() methods to validate and receive a converted Date value.

Implementations of the validate() method are provided to create Date objects for different time zones if the system default is not appropriate.

Once a value has been sucessfully converted the following methods can be used to perform various date comparison checks:

  • compareDates() compares the day, month and year of two dates, returing 0, -1 or +1 indicating whether the first date is equal, before or after the second.
  • compareWeeks() compares the week and year of two dates, returing 0, -1 or +1 indicating whether the first week is equal, before or after the second.
  • compareMonths() compares the month and year of two dates, returing 0, -1 or +1 indicating whether the first month is equal, before or after the second.
  • compareQuarters() compares the quarter and year of two dates, returing 0, -1 or +1 indicating whether the first quarter is equal, before or after the second.
  • compareYears() compares the year of two dates, returing 0, -1 or +1 indicating whether the first year is equal, before or after the second.

So that the same mechanism used for parsing an input value for validation can be used to format output, corresponding format() methods are also provided.

DateValidatorTest.javaClass Test Case for DateValidator.
DoubleValidator.javaClass

Double Validation and Conversion routines (java.lang.Double).

This validator provides a number of methods for validating/converting a String value to a Double using java.text.NumberFormat to parse either:

  • using the default format for the default Locale
  • using a specified pattern with the default Locale
  • using the default format for a specified Locale
  • using a specified pattern with a specified Locale

Use one of the isValid() methods to just validate or one of the validate() methods to validate and receive a converted Double value.

Once a value has been sucessfully converted the following methods can be used to perform minimum, maximum and range checks:

  • minValue() checks whether the value is greater than or equal to a specified minimum.
  • maxValue() checks whether the value is less than or equal to a specified maximum.
  • isInRange() checks whether the value is within a specified range of values.

So that the same mechanism used for parsing an input value for validation can be used to format output, corresponding format() methods are also provided.

DoubleValidatorTest.javaClass Test Case for DoubleValidator.
FloatValidator.javaClass

Float Validation and Conversion routines (java.lang.Float).

This validator provides a number of methods for validating/converting a String value to a Float using java.text.NumberFormat to parse either:

  • using the default format for the default Locale
  • using a specified pattern with the default Locale
  • using the default format for a specified Locale
  • using a specified pattern with a specified Locale

Use one of the isValid() methods to just validate or one of the validate() methods to validate and receive a converted Float value.

Once a value has been sucessfully converted the following methods can be used to perform minimum, maximum and range checks:

  • minValue() checks whether the value is greater than or equal to a specified minimum.
  • maxValue() checks whether the value is less than or equal to a specified maximum.
  • isInRange() checks whether the value is within a specified range of values.

So that the same mechanism used for parsing an input value for validation can be used to format output, corresponding format() methods are also provided.

FloatValidatorTest.javaClass Test Case for FloatValidator.
IntegerValidator.javaClass

Integer Validation and Conversion routines (java.lang.Integer).

This validator provides a number of methods for validating/converting a String value to a Integer using java.text.NumberFormat to parse either:

  • using the default format for the default Locale
  • using a specified pattern with the default Locale
  • using the default format for a specified Locale
  • using a specified pattern with a specified Locale

Use one of the isValid() methods to just validate or one of the validate() methods to validate and receive a converted Integer value.

Once a value has been sucessfully converted the following methods can be used to perform minimum, maximum and range checks:

  • minValue() checks whether the value is greater than or equal to a specified minimum.
  • maxValue() checks whether the value is less than or equal to a specified maximum.
  • isInRange() checks whether the value is within a specified range of values.

So that the same mechanism used for parsing an input value for validation can be used to format output, corresponding format() methods are also provided.

IntegerValidatorTest.javaClass Test Case for IntegerValidator.
LongValidator.javaClass

Long Validation and Conversion routines (java.lang.Long).

This validator provides a number of methods for validating/converting a String value to a Long using java.text.NumberFormat to parse either:

  • using the default format for the default Locale
  • using a specified pattern with the default Locale
  • using the default format for a specified Locale
  • using a specified pattern with a specified Locale

Use one of the isValid() methods to just validate or one of the validate() methods to validate and receive a converted Long value.

Once a value has been sucessfully converted the following methods can be used to perform minimum, maximum and range checks:

  • minValue() checks whether the value is greater than or equal to a specified minimum.
  • maxValue() checks whether the value is less than or equal to a specified maximum.
  • isInRange() checks whether the value is within a specified range of values.

So that the same mechanism used for parsing an input value for validation can be used to format output, corresponding format() methods are also provided.

LongValidatorTest.javaClass Test Case for LongValidator.
PercentValidator.javaClass

Percentage Validation and Conversion routines (java.math.BigDecimal).

This is one implementation of a percent validator that has the following features:

  • It is lenient about the the presence of the percent symbol
  • It converts the percent to a java.math.BigDecimal

However any of the number validators can be used for percent validation. For example, if you wanted a percent validator that converts to a java.lang.Float then you can simply instantiate an FloatValidator with the appropriate format type:

...

PercentValidatorTest.javaClass Test Case for PercentValidator.
RoutinesTestSuite.javaClass Test suite for org.apache.commons.validator.routines package.
ShortValidator.javaClass

Short Validation and Conversion routines (java.lang.Short).

This validator provides a number of methods for validating/converting a String value to a Short using java.text.NumberFormat to parse either:

  • using the default format for the default Locale
  • using a specified pattern with the default Locale
  • using the default format for a specified Locale
  • using a specified pattern with a specified Locale

Use one of the isValid() methods to just validate or one of the validate() methods to validate and receive a converted Short value.

Once a value has been sucessfully converted the following methods can be used to perform minimum, maximum and range checks:

  • minValue() checks whether the value is greater than or equal to a specified minimum.
  • maxValue() checks whether the value is less than or equal to a specified maximum.
  • isInRange() checks whether the value is within a specified range of values.

So that the same mechanism used for parsing an input value for validation can be used to format output, corresponding format() methods are also provided.

ShortValidatorTest.javaClass Test Case for ShortValidator.
TimeValidator.javaClass

Time Validation and Conversion routines (java.util.Calendar).

This validator provides a number of methods for validating/converting a String time value to a java.util.Calendar using java.text.DateFormat to parse either:

  • using the default format for the default Locale
  • using a specified pattern with the default Locale
  • using the default format for a specified Locale
  • using a specified pattern with a specified Locale

For each of the above mechanisms, conversion method (i.e the validate methods) implementations are provided which either use the default TimeZone or allow the TimeZone to be specified.

Use one of the isValid() methods to just validate or one of the validate() methods to validate and receive a converted Calendar value for the time.

Implementations of the validate() method are provided to create Calendar objects for different time zones if the system default is not appropriate.

Alternatively the CalendarValidator's adjustToTimeZone() method can be used to adjust the TimeZone of the Calendar object afterwards.

Once a value has been sucessfully converted the following methods can be used to perform various time comparison checks:

  • compareTime() compares the hours, minutes, seconds and milliseconds of two calendars, returing 0, -1 or +1 indicating whether the first time is equal, before or after the second.
  • compareSeconds() compares the hours, minutes and seconds of two times, returing 0, -1 or +1 indicating whether the first is equal to, before or after the second.
  • compareMinutes() compares the hours and minutes two times, returing 0, -1 or +1 indicating whether the first is equal to, before or after the second.
  • compareHours() compares the hours of two times, returing 0, -1 or +1 indicating whether the first is equal to, before or after the second.

So that the same mechanism used for parsing an input value for validation can be used to format output, corresponding format() methods are also provided.

TimeValidatorTest.javaClass Test Case for TimeValidator.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.