Welcome Guest, Not a member yet? Register   Sign In
Form validation and non-english characters?
#1

[eluser]axed[/eluser]
Hi,

I'm using the Form validation class to check an input field. The rule for the field is required|alpha and it works without any problems when I'm typing in english.
But when I'm trying to input some Swedish characters (å ä and ö) the validation fails with the message: The ... field may only contain alphabetical characters.
What can i do to solve this?
I mean the 'å', 'ä', and 'ö' -signs are all alphabetical characters in Sweden.
I'm using UTF-8...
#2

[eluser]danmontgomery[/eluser]
[quote author="axed" date="1264733632"]Hi,

I'm using the Form validation class to check an input field. The rule for the field is required|alpha and it works without any problems when I'm typing in english.
But when I'm trying to input some Swedish characters (å ä and ö) the validation fails with the message: The ... field may only contain alphabetical characters.
What can i do to solve this?
I mean the 'å', 'ä', and 'ö' -signs are all alphabetical characters in Sweden.
I'm using UTF-8...[/quote]

Alpha is considered only a-z and A-Z... If you want anything else you'd have to use a callback function.

http://ellislab.com/codeigniter/user-gui...#callbacks
#3

[eluser]jmadsen[/eluser]
I haven't tried this, but wonder if this is correct?

The basic form validation rules are in library Validation (edit: actually, Form_validation now. Whichever you are using.), so if you need to use an expanded alphabet on a regular basis, try extending this class and making a "swed_alpha" rule that would include your special characters.

I guess that's no different than calling a helper function. Anyone explain advantage/disadvantage of one over the other?
#4

[eluser]tomcode[/eluser]
Right now I work like that :

file librairies/MY_Form_validation.php encoded in UTF-8
Code:
/**
     * Validation callback, allows international text
     *
     * @param    string
     * @return    bool
     */    
    function alpha_int($str)
    {
        $str = (strtolower($this->CI->config->item('charset')) != 'utf-8') ? utf8_encode($str) : $str;

        return ( ! preg_match("/^[[:alpha:]- ÀÁÂÃÄÅĀĄĂÆÇĆČĈĊĎĐÈÉÊËĒĘĚĔĖĜĞĠĢĤĦÌÍÎÏĪĨĬĮİIJĴĶŁĽĹĻĿÑŃŇŅŊÒÓÔÕÖØŌŐŎŒŔŘŖŚŠŞŜȘŤŢŦȚÙÚÛÜŪŮŰŬŨŲŴÝŶŸŹŽŻàáâãäåæçèéêëìíîïñòóôõöøùúûüýÿœšß_.]+$/", $str)) ? FALSE : TRUE;
    }
You might need to add some chars ...


language files fom_validation_lang.php
Code:
// MY_Form_validation
$lang['alpha_int']        = "The %s field may only contain text.";

Another possibility I once coded, if I remember right, was using mb_string and set_locale.
#5

[eluser]saidai jagan[/eluser]
Thanx. tomcode Smile
#6

[eluser]axed[/eluser]
Thank you all!




Theme © iAndrew 2016 - Forum software by © MyBB