Validating user's UTF-8 name in Javascript

Viewed 32528

I am using the following JavaScript regexp to validate users First and Last names

var regexp = /^((?=[a-z \']).)+$/i;

var val1 = "Normal Text' Compromised";       // true
var val2 = "UTF Text' Połącz Słońce w Mózu"; // false  <---- UTF-8
var val3 = "Illegal char: Blac & White";     // false

example at: http://jsfiddle.net/PR4T2/1/


Question:

Is there any way to make the regex "UTF-8 insensitive" in order to let users use UTF characters ?

I know that UTF is not supported in JS validation yet but I was wondering if there is any workaround. I also do not want to exclude all illegal characters manually like:

var regexp = /^((?![0-9\~\!\@\#\$\%\^\&\*\(\)\_\+\=\-\[\]\{\}\;\:\"\\\/\<\>\?]).)+$/;

Edit:

Criteria are characters: a-z, space, \, ' and any other UTF char that can be found in user First/Last Name like here.

I'm looking for something more general like \p{xx} sequencer in PHP

4 Answers

Another interesting way of doig this, is using Lodash's _.deburr(string) on the string before validating it normally :)

The deburr method will convert latin characters such as é to e, while preserving characters like $, @, &...

I believe this should be enough for most latin-derived languages.

Some examples:

Input:

var str1 = _.deburr("ÀÄÅÆÇÈÉ");
console.log(str1);
  
var str2 = _.deburr("ãäåæçè");
console.log(str2);

Output:

"AAAAeCEE"
"aaaaece"

and

Input:

var str1 = _.deburr("ĜĚĔĶŜ");
console.log(str1);

var str2 = _.deburr("ĝęėķś");
console.log(str2);

Output:

"GEEKS"
"geeks"
Related