www.webdeveloper.com
Results 1 to 7 of 7

Thread: form validation for international characters

  1. #1
    Join Date
    Oct 2010
    Posts
    3

    form validation for international characters

    I am trying to build a form that validates input on the name field and I want to disallow certain characters such as commas, question marks, semicolon, star, backslash etc. I know I can use preg_match and set [a-zA-Z] but when I do that international characters are not allowed anymore. I tried something below but it gives error.

    PHP Code:
    if (preg_match("/[.\:\;\?\ ]/",$name)) { 
                 
    $hasError true;
                 } 
    what should I do? is there any other function that I can use?

    thanks in advance

  2. #2
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    PHP Code:
    if (preg_match("#(*UTF8)/[.\:\;\?\ ]/#",$name)) {
                 
    $hasError true;
                 } 
    Something like this, I'm not a big fan of working with regex or utf8. But the regex function has some documentation (from users.... ikr) on using regex in utf8.
    Last edited by eval(BadCode); 10-27-2010 at 08:21 PM.

  3. #3
    Join Date
    Oct 2010
    Posts
    3
    Hi eval(BadCode,

    that code gave the following error

    Warning: preg_match() [function.preg-match]: Compilation failed: (*VERB) not recognized at offset 5 in ...

  4. #4
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    PHP Code:
    if (preg_match("/(*UTF8)[.\:\;\?\ ]/",$name)) {
                 
    $hasError true;
                 } 
    That works. Just a simple matter of adding (*UTF8) at the beginning of your regular expression. # can be used instead of / as doing so automatically escapes the expression if you're wondering about that.

    If you find this doesn't work for whatever reason (older PHP or PCRE) you can also try utf8_decode($name) in the preg_match expression.

    -jim

  5. #5
    Join Date
    Oct 2010
    Posts
    3
    it didnt work again and I tried utf8_decode($name) and add a backslash before first dot.

    PHP Code:
    if (preg_match("/[\.\:\;\?\ ]/"utf8_decode($name)))    
    {
                 
    $hasError true
                 } 
    so far so good!

    Thanks for your help.

  6. #6
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    You can always convert the unicode string to hex or binary and search it like that, then convert it back.

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,614
    How about the lower-case "u" pattern modifier?
    PHP Code:
    if(preg_match('/[.:; ]/u'$name)) { 
    ???

    If all else fails, you could try the mb_ereg_match() function, if the MBString extension is enabled.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles