1

I have feature to add wesbsite option in form.
Here user can write domain /url and this domain/url can be in English as well Japanese language as below.
www.google.com
www.南极星.com

I am using following validation for english domains    

        for (var j = 0; j < dname.length; j++) {

            var dh = dname.charAt(j);

            var hh = dh.charCodeAt(0);

            /*if(dh!='.'){
             var chkip=chkip+dh;
             }*/
            if ((hh > 47 && hh < 59) || (hh > 64 && hh < 91) || (hh > 96 && hh < 123) || hh == 45 || hh == 46) {

                var index2 = dname.indexOf('www.');
                if (index2 != -1) {
                    dname = dname.substring(index2 + 4);
                    if (dname.charAt(0) == '-') {
                        error_msg = '\'-\'' + window.gt.gettext('not_allowed_in_beginning');
                        return error_msg;
                    }
                }
                if ((j == 0 || j == dname.length - 1) && hh == 45) {
                    //if(hh == 45){
                    error_msg = '\'-\'' + window.gt.gettext('not_allowed_in_beginning');

                }
            } else {

                error_msg = window.gt.gettext('cmnscrpt_domname_inval');
            }
        }

what can I write to validate Japanese domain ?

2 Answers2

0

I think you should use form validator. For example, I prefer to use this one: http://jqueryvalidation.org/validate. You can write your own validation rules depending on language.

For exanple, this is how you can validate car VIN number:

(function() {
    jQuery.validator.addMethod("vin", function(value, element) {
        return this.optional(element) || /^[a-z0-9]{17}$/i.test(value);
    }, "");
})();
0

There is a very simple method to apply all you RegEx logic(that one can apply easily in English) for any Language using Unicode.

For matching a range of Unicode Characters like all Alphabets [A-Za-z] we can use

[\u0041-\u005A] where \u0041 is Hex-Code for A and \u005A is Hex Code for Z

'matchCAPS leTTer'.match(/[\u0041-\u005A]+/g)
//output ["CAPS", "TT"]

In the same way we can use other Unicode characters or their equivalent Hex-Code according to their Hexadecimal Order (eg: \u0A10 to \u0A1F) provided by unicode.org

Below is a regEx for url validation

url.match(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/)

you just replace a-z, A-Z, 0-9 with the similar characters from Japanese Unicode set and it will work fine. I don't know Japanese :)

Harpreet Singh
  • 2,651
  • 21
  • 31