Baeuerle Associates, LLC
 5570 Leeward Ln
 Tilghman, MD 21671

Regular Expression

Usage of Regular Expression for validation. Back to Validation

Samples:

class ValidateWithRegularExpression implements Serializable {

    BigDecimal amount
    String usPhone
    String usZipCode
    String number
    String integer
    String time
    String ipAddress
    String ssn


    static constraints = {
      //validation amount: 999.99 or $999.99
      amount(nullable:false, matches:/((\$\d*\.\d{2})|(\d*\.\d{2}))$/)

      //validation US phone patterns (999) 999-9999 or (999)999-9999
      usPhone(blank:false, matches:/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/)

      //validation of numbers (numeric characters)
      number(blank:false, matches:/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/)

      //validation of integer numbers
      integer(blank:false, matches:/(^-?\d\d*$)/)

       //validation of US zip code 99999 or 99999-9999
      usZipCode(blank:false, matches:/(^\d{5}$)|(^\d{5}-\d{4}$)/)

      //validation of time as a string HH:MM or HH:MM:SS or HH:MM:SS.mmm
      time(blank:false, matches:/^([1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$/)

      //validation of IP Address(no check for alid values (0-255)) 999.999.999.999
      ipAddress(blank:false, matches:/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)

      //validation of Social Security Number 999-99-9999 or999999999
      ssn(blank:false, matches:/^\d{3}\-?\d{2}\-?\d{4}$/)

    }

}

More to Regular Expression: www.regular-expression.info

Learning Regular Expression:
www.zytrax.com/tech/web/regex.htm
gnosis.cx/publish/programming/regularExpression.html

Back to
Validation
 

[Home] [Company] [Services] [Grails] [Grails beyond CRUD] [Domain] [Controller] [View] [Validation] [Links]

For additional questions please eMail to Baeuerle Associates, LLC
©Copyright 2003 Baeuerle Associates, LLC. All rights reserved.