OK,
I needed to to be able to validate a zero value (0) or a valid integer with a max length of 11 digits for a
parent_id field in a nested table (like com_categories table) that I'm working on...
This is how:
^([0-9]{1,1}|[1-9]([0-9]{1,10}))$
How it works:
- The outer parentheses '()' allow separate arguments
- The pipe '|' denotes an OR statement
- Square brackets denote acceptable characters
- Curly braces '{val1, val2}' specify, upper (val2) and lower (val1) length
So, this regex basically says,
- IF the first character matches anything between 0 and 9 ([0-9]) and is only 1 digit long ({1,1}) OR (|)
- IF the first character matches anything between 1 and 9 ([1-9]) AND any subsequent characters are between 0 and 9 to a total length of 10 and a minimum of 1 then... RETURN TRUE
ELSE
RETURN FALSE
**N.B.We only check for a length of 10 as we are only checking everything after the first character in the second case. There are 11 in total - the maximum length of ID fields in the DB**
Hope this Helps!!!
Gez