Illasera Posted May 27, 2010 Share Posted May 27, 2010 am looking for a function or a way/method to report an error once a unicode character(NONE-ASCII) found in a string.I have a registration form and i don`t want users to be able to enter unicode passwords(none-ascii), how do i report an error was unicode character within a string was found?I couldn`t find the right filter function for it...Thanks in advance. Link to comment Share on other sites More sharing options...
Ingolme Posted May 27, 2010 Share Posted May 27, 2010 What do you consider a unicode character? The Unicode character set includes every single character that can be typed on a computer. That also includes A, B, C...If you only want letters, numbers and underscores you can use a regular expression, something like this: [a-zA-Z0-9_]+ Link to comment Share on other sites More sharing options...
Illasera Posted May 27, 2010 Author Share Posted May 27, 2010 What do you consider a unicode character? The Unicode character set includes every single character that can be typed on a computer. That also includes A, B, C...If you only want letters, numbers and underscores you can use a regular expression, something like this: [a-zA-Z0-9_]+ Not ascii a-z/A-Z, 0-9, yep,But what do you mean "use a regular expression"?should i write a "for" loop that iterate through the strings and check for bad characters?I was hoping to find a function that does it for me... Link to comment Share on other sites More sharing options...
jeffman Posted May 27, 2010 Share Posted May 27, 2010 The best thing is to use a regular expression that allows ONLY permitted characters. So function isValid($str) { $re = '/[^a-zA-Z0-9]/'; // add permitted characters between the [braces] return !preg_match($re, $str);}// small test$password = "mypassword";if (isValid($password) ) { echo 'Valid';}$password = "mypésswürd";if (isValid($password) ) { echo 'Valid';} else { echo 'Not valid';} I wrote this mostly from memory. Be sure to test it. Link to comment Share on other sites More sharing options...
chibineku Posted May 27, 2010 Share Posted May 27, 2010 Not ascii a-z/A-Z, 0-9, yep,But what do you mean "use a regular expression"?should i write a "for" loop that iterate through the strings and check for bad characters?I was hoping to find a function that does it for me...That is precisely what a regular expression does. It's a pattern against which a string is tested. They're extremely useful but can be intimidating at first because of the syntax. php regex and you'll find a lot of documentation on the functions that use it. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.