JavaScript validation issue with international characters

前端 未结 7 1302
粉色の甜心
粉色の甜心 2020-12-02 07:35

We use the excellent validator plugin for jQuery here on Stack Overflow to do client-side validation of input before it is submitted to the server.

It generally work

相关标签:
7条回答
  • 2020-12-02 08:12

    Use something like Fiddler or Charles (not Firebug's Net panel, or anything else that's actually inside the browser) to examine what's actually coming over the wire. It's almost certainly an encoding issue: either the file has been saved in some Microsoft character set and is being sent as UTF-8, or maybe the other way around.

    In the case of JS RegExps you can, as Boldewyn points out, avoid these problems by specifying the Unicode code point for the characters you want that are outside the US-ASCII range. It would still be as well to make sure you aren't mixing up encodings between the place where the file is saved and the place where it's served, though.

    0 讨论(0)
  • 2020-12-02 08:17

    What is the character encoding of the JS file?

    For XML QNames I use this RegExp:

    /**
     * Definition of an XML Name
     */
    var NameStartChar = "A-Za-z:_\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D"+
                        "\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF"+
                        "\uF900-\uFDCF\uFDF0-\uFFFD\u010000-\u0EFFFF";
    var NameChar = NameStartChar+"\\-\\.0-9\u00B7\u0300-\u036F\u203F-\u2040";
    var Name = "^["+NameStartChar+"]["+NameChar+"]*$";
    RegExp (Name).test (value);
    

    It works like a charm also with internationalized characters. Note the escaping. Due to that I'm able to restrict the JS file to ASCII characters only. Therefore I don't get into trouble when dealing with ISO-8859 vs UTF-8 charsets.

    This is no more true, if you use character encodings where ASCII is no real subset (like, e.g., in Asia UTF-16).

    Cheers,

    0 讨论(0)
  • 2020-12-02 08:18

    This isn't really an answer but I don't have 50 rep yet to add a comment... It can definately be attributed to encoding issues.

    Yea "ECMA shouldn't care about encoding..." blah blah, well if you're on firefox, go to View > Character Encoding > Western (ISO-8859-1) then try using the Name field.

    It works fine for me after changing the encoding manually (granted the rest of the page doesn't like the encoding switch, :P)

    (on IE8 you can go to Page > Encoding > Western European (Windows) to get the same effect)

    0 讨论(0)
  • 2020-12-02 08:18

    Seeing as the statement works in the console, could this have to do the way your .js files are saved (i.e. ascii or UTF-8) and that the browser is loading them thusly and in the process translates the characters?

    0 讨论(0)
  • 2020-12-02 08:21

    international characters listed are part of extended ASCII. the ones added by you are certainly not.

    0 讨论(0)
  • 2020-12-02 08:30

    Late to the game here, but I just used this expression and it seemed to work well for me. Seems to be fairly comprehensive and relatively simple:

    var re = /^[A-zÀ-Ÿ\s\d-]*$/g; 
    var str1 = 'casa-me,pois 99 estou farto! Eis a lista:uma;duas;três';
    var str2 = 'casa-me pois 99 estou farto Eis a lista uma duas três';
    var str3 = 'àèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸçÇߨøÅ寿œ'
    
    alert(re.test(str1));
    alert(re.test(str2));
    alert(re.test(str3));

    0 讨论(0)
提交回复
热议问题