Welcome Guest, Not a member yet? Register   Sign In
Form_validation regex_match
#1

[eluser]Exangelus[/eluser]
Hi all,

In the CI_Form_validation library (v201) I found (to my surprise) a regular expression validation method, however I'm running into some trouble..

I wanna validate a date using the a regular expression, I got this one
Code:
^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$
From http://www.regular-expressions.info/dates.html

I've also tried this (escaping the pipes and square brackets):
Code:
regex_match[^(0\[1-9\]\|\[12\]\[0-9\]\|3\[01\])\[- /.\](0\[1-9\]\|1\[012\])\[- /.\](19\|20)\d\d$]

Now when I simply add: regex_match[^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$] to my syntax, this doesn't work. I tried escaping characters with \ but this did not work neither, anyone got a handle how to correctly validate a regular expression?

Thx in advance!
#2

[eluser]InsiteFX[/eluser]
Use a Form_Validation Callback and check your date using regex in it.

InsiteFX
#3

[eluser]Exangelus[/eluser]
That's a plausible work around but as the function already exists in the default Form_validation it should work somehow and we strongly prefer working without work-arounds whenever possible. But still thanks for the tip! It's at least a temporarily solution.
#4

[eluser]InsiteFX[/eluser]
Regex help application:

Regex online

If you look at the bottom right hand side you can dowload a desktop version!

InsiteFX
#5

[eluser]Unknown[/eluser]
The function currently built into the form_validator library will only work for regular expressions NOT containing square brackets aka character classes.

There's a problem in Line #116 where it tries to match the array brackets in the supplied validation/prepping function.

Code:
// Is the field name an array?  We test for the existence of a bracket "[" in
// the field name to determine this.  If it is an array, we break it apart
// into its components so that we can fetch the corresponding POST data later
if (strpos($field, '[') !== FALSE AND preg_match_all('/\[(.*?)\]/', $field, $matches))

You might notice it matches the parameter given in square brackets, but only until the first closing square bracket. (Non-greedy matching by ? at the end of .*) If you use character classes in your expression, the first closing bracket of the first character class is terminating the match and therefore no ending delimiter will be found for your regex rendering it useless.

Other than that this function would be a great addition to the validation rules.
#6

[eluser]Exangelus[/eluser]
I already worked that out and I'm currently using this code:
Code:
if(stripos($rule, '[') !== false && substr($rule, -1) == ']')

However, simple regexp do seem to work while more complex one's do not. I'm still figuring out with which exact characters (or combinations) it goes wrong..

Cheers!




Theme © iAndrew 2016 - Forum software by © MyBB