views:

19

answers:

2

I need a javascript function for validating alphabetic input with a few extra characters from the Italian languages, namely: àèéìòóù

I saw a regex before with something like:

[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]

What are those codes named and where can I find the right values?
I would like to use these into following function:

function val_alpha_it(str) {
    var re = /[^A-Za-z]/
    return re.test(str);                
}
A: 

If the encoding works fine for your script, you can just put the characters in the negative set in the regular epxression:

function val_alpha_it(str) {
  return /[^A-Za-zàèéìòóù]/.test(str);
}

If you need to specify the characters using character codes, those are unicode code points. You can look them up in the Unicode character set. The character à for example has character code 00E0, so you write that as \u00E0. So, with character code escapes, the code would be:

function val_alpha_it(str) {
  return /[^A-Za-z\u00E0\u00E8\u00E9\u00EC\u00F2\u00F3\u00F9]/.test(str);
}
Guffa
Are you sure this works? On regular-expressions.info, it says for JS: *No Unicode support, except for matching single characters with \uFFFF* (have not tested it myself that is why I ask).
Felix Kling
@Felix: It works in some browsers at least, like Firefox, but you are probably right that there is no global support, so it would be safer to use the character codes.
Guffa
Perfect. Cheers Guffa!
FFish
+1  A: 

Here's a good tutorial on Unicode in Regex: http://www.regular-expressions.info/unicode.html

As you mentioned, JavaScript only supports the \xFFFF style syntax, and that page lists the various ranges of characters.

If you need a specific character, you can use Character Map on Windows to lookup Unicode character codes (look for the U+FFFF in the bottom left, and change to the syntax above).

Peter Boughton