简体   繁体   中英

convert prototype script to jquery script

bottom code convert digit in text input to Farsi language digit and mirror .

I need convert prototype to jquery,please help me

 String.prototype.toFaDigit = function() {
        return this.replace(/\d+/g, function(digit) {
            var ret = '';
            for (var i = 0, len = digit.length; i < len; i++) {
                ret += String.fromCharCode(digit.charCodeAt(i) + 1728);
            }

            return ret;
        });
    };

    String.prototype.toEnDigit = function() {
    return this.replace(/[\u06F0-\u06F9]+/g, function(digit) {
        var ret = '';
        for (var i = 0, len = digit.length; i < len; i++) {
            ret += String.fromCharCode(digit.charCodeAt(i) - 1728);
        }

        return ret;
    });
};
 function ChekNumLang_ChekBox(MainTextFieldName) {
    var fieldObj = document.getElementById(MainTextFieldName);
    if (document.getElementById("FarsiNum").checked) {
        fieldObj.value = fieldObj.value.toFaDigit();
    }
    else {
        fieldObj.value = fieldObj.value.toEnDigit();
    }

If it is upto me I'll be doing something like

StringUtils = {};

StringUtils.toFaDigit = function(string) {
    return string.replace(/\d+/g, function(digit) {
                var ret = '';
                for (var i = 0, len = digit.length; i < len; i++) {
                    ret += String.fromCharCode(digit.charCodeAt(i) + 1728);
                }

                return ret;
            });
};

StringUtils.toEnDigit = function(string) {
    return string.replace(/[\u06F0-\u06F9]+/g, function(digit) {
                var ret = '';
                for (var i = 0, len = digit.length; i < len; i++) {
                    ret += String.fromCharCode(digit.charCodeAt(i) - 1728);
                }

                return ret;
            });
};

function ChekNumLang_ChekBox(MainTextFieldName) {
    var fieldObj = $('#' + MainTextFieldName)
    if ($('#FarsiNum').is(':checked')) {
        fieldObj.val(StringUtils.toFaDigit(fieldObj.val()));
    } else {
        fieldObj.val(StringUtils.toEnDigit(fieldObj.val()));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM