grailsconstraintsgrails-ormgrails-constraints

regex validation - grails constraints


I'm pretty new on grails, I'm having a problem in matches validation using regex. What I wanted to happen is my field can accept a combination of alphanumeric and specific special characters like period (.), comma (,) and dash (-), it may accept numbers (099) or letters only (alpha) , but it won't accept input that only has special characters (".-,"). Is it possible to filter this kind of input using regex? please help. Thank you for sharing your knowledge.


Solution

  • ^[0-9a-zA-Z,.-]*?[0-9a-zA-Z]+?[0-9a-zA-Z,.-]*$
    
    meaning:
      /
      ^          beginning of the string
      [...]*?    0 or more characters from this class (lazy matching)
      [...]+?    1 or more characters from this class (lazy matching)
      [...]*     0 or more characters from this class
      $          end of the string
      /