Validation of Letters in Codeigniter


I am developing an application in

asked by J. Castro 22.09.2016 в 16:04

1 answer


By using alpha as the first rule, you are limiting the entry to only letters. With this rule you are forbidding spaces, commas or points to enter. You have to eliminate that rule:

$crud->set_rules( 'marca', 'Marca', 'callback_solo_letras' );

On the other hand, the regular expression should

  • be anchored at the beginning of the text with ^ .
  • only allow spaces (since \s allows any blank space or [ \t\r\n\f] )
  • /^[a-z ,.]*$/i

    And the function would be:

    public function solo_letras($cadena)
        return preg_match( '/^[a-z ,.]*$/i', $cadena );

    However, it is much easier to use the rule regex_match[/regex/]

    $crud->set_rules( 'marca', 'Marca', 'regex_match[/^[a-z ,.]*$/i]' );
    • Or if you want to allow ñ and other letters of Spanish:

      $crud->set_rules( 'marca', 'Marca', 'regex_match[/^[a-zñáéíóúüA-ZÑÁÉÍÓÚÜ ,.]*$/u]' );
    • Or any letter of any alphabet (general category Letter from Unicode).

      $crud->set_rules( 'marca', 'Marca', 'regex_match[/^[\p{L} ,.]*$/u]' );
    answered by 24.09.2016 / 12:19