CodeIgniter Forums
Form Validation and Regexp - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Form Validation and Regexp (/thread-43870.html)



Form Validation and Regexp - El Forum - 07-26-2011

[eluser]Unknown[/eluser]
Hello I need to set_rules in Form Validation to regexp pattern from database:

I used callback function but it doesn't working.

How can I use it? I found it in form_validation:
Code:
/**
     * Performs a Regular Expression match test.
     *
     * @access    public
     * @param    string
     * @param    regex
     * @return    bool
     */
    function regex_match($str, $regex)
    {
        if ( ! preg_match($regex, $str))
        {
            return FALSE;
        }

        return  TRUE;
    }

But I don't know how to use it.


Form Validation and Regexp - El Forum - 07-26-2011

[eluser]Unknown[/eluser]
You can use it the same way you would use any other validation rule.

Code:
$this->form_validation->set_rules('field name', 'human name', 'regex_match[expression]');

Replace 'expression' with the regular expression from your database.

http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#validationrules

However, if your regular expression contains square brackets then you need to use a callback.

There is some more info in this thread: http://ellislab.com/forums/viewthread/185601/

I am not sure why this validation function isn't documented in the user guide. Can anyone provide any insight?


Form Validation and Regexp - El Forum - 07-27-2011

[eluser]toopay[/eluser]
[quote author="ReptileReX" date="1311706107"]How can I use it? ...
But I don't know how to use it.[/quote]

[quote author="birderic" date="1311714118"]I am not sure why this validation function isn't documented in the user guide. Can anyone provide any insight?[/quote]

Mocking the original userguide...

Rule : regex_match
Parameter : YES
Description : Returns FALSE if the form element is not match with regular expression pattern.
Example : regex_match[/^SELECT ([^)]+) FROM ([^)]+)$/i]

(Above example will examinate, if valid SELECT statement present.)

What wrong with you guys? Code is as is. You see above function is just a shorthand to perform regular expression, and you can use it just the way you use preg_match().