Merge branch 'feature/fcs' into 'release/5.7'
中文字符相当于2个字符 See merge request !590
Showing
3 changed files
with
61 additions
and
5 deletions
public/js/common/helpers/stringHandle.js
0 → 100644
1 | +/* | ||
2 | + * @Author: Targaryen | ||
3 | + * @Date: 2017-05-22 16:48:15 | ||
4 | + * @Last Modified by: Targaryen | ||
5 | + * @Last Modified time: 2017-05-22 16:53:18 | ||
6 | + */ | ||
7 | +let stringHandle = { | ||
8 | + | ||
9 | + /** | ||
10 | + * 汉字相当于两个字符的字符串长度计算 | ||
11 | + * @param {*} str | ||
12 | + */ | ||
13 | + chinese2(str) { | ||
14 | + let total = 0, | ||
15 | + charCode, | ||
16 | + i, | ||
17 | + len; | ||
18 | + | ||
19 | + for (i = 0, len = str.length; i < len; i++) { | ||
20 | + charCode = str.charCodeAt(i); | ||
21 | + if (charCode <= 0x007f) { | ||
22 | + total += 1; | ||
23 | + } else { | ||
24 | + total += 2; | ||
25 | + } | ||
26 | + } | ||
27 | + return total; | ||
28 | + }, | ||
29 | + | ||
30 | + /** | ||
31 | + * 中文相当于两个字符情况下的字符串截取 | ||
32 | + * @param {*} str | ||
33 | + * @param {*} n | ||
34 | + */ | ||
35 | + sub_chinese2(str, n) { | ||
36 | + let r = /[\u4E00-\u9FA5]/g; | ||
37 | + | ||
38 | + if (str.replace(r, 'mm').length <= n) { | ||
39 | + return str; | ||
40 | + } | ||
41 | + | ||
42 | + let m = Math.floor(n / 2); | ||
43 | + | ||
44 | + for (let i = m; i < str.length; i++) { | ||
45 | + if (str.substr(0, i).replace(r, 'mm').length >= n) { | ||
46 | + return str.substr(0, i); | ||
47 | + } | ||
48 | + } | ||
49 | + return str; | ||
50 | + } | ||
51 | +}; | ||
52 | + | ||
53 | +module.exports = stringHandle; |
@@ -3,11 +3,13 @@ | @@ -3,11 +3,13 @@ | ||
3 | * @author: bikai<kai.bi@yoho.cn> | 3 | * @author: bikai<kai.bi@yoho.cn> |
4 | * @date: 2015/11/30 | 4 | * @date: 2015/11/30 |
5 | */ | 5 | */ |
6 | -let $ = require('yoho-jquery'), | 6 | +const $ = require('yoho-jquery'), |
7 | tip = require('plugin/tip'), | 7 | tip = require('plugin/tip'), |
8 | security = require('plugin/security'), | 8 | security = require('plugin/security'), |
9 | loading = require('plugin/loading'); | 9 | loading = require('plugin/loading'); |
10 | 10 | ||
11 | +const stringHandle = require('common/helpers/stringHandle'); | ||
12 | + | ||
11 | let $addressForm = $('.edit-address'), | 13 | let $addressForm = $('.edit-address'), |
12 | $submit = $('.submit'), | 14 | $submit = $('.submit'), |
13 | $editAddressPage = $('.my-edit-address-page'), | 15 | $editAddressPage = $('.my-edit-address-page'), |
@@ -60,9 +62,9 @@ $backBtn.on('touchend', function(e) { | @@ -60,9 +62,9 @@ $backBtn.on('touchend', function(e) { | ||
60 | }); | 62 | }); |
61 | 63 | ||
62 | $usernameInput.bind('input propertychange', function() { | 64 | $usernameInput.bind('input propertychange', function() { |
63 | - if ($usernameInput.val().length > 20) { | 65 | + if (stringHandle.chinese2($usernameInput.val()) > 20) { |
64 | tip.show('收货人姓名最多支持20个字'); | 66 | tip.show('收货人姓名最多支持20个字'); |
65 | - $usernameInput.val($usernameInput.val() && $usernameInput.val().substring(0, 20)); | 67 | + $usernameInput.val($usernameInput.val() && stringHandle.sub_chinese2($usernameInput.val(), 20)); |
66 | } | 68 | } |
67 | }); | 69 | }); |
68 | 70 |
@@ -4,6 +4,7 @@ require('home/order-address-modify.page.css'); | @@ -4,6 +4,7 @@ require('home/order-address-modify.page.css'); | ||
4 | const $ = require('yoho-jquery'); | 4 | const $ = require('yoho-jquery'); |
5 | const Vue = require('vue'); | 5 | const Vue = require('vue'); |
6 | const tip = require('plugin/tip'); | 6 | const tip = require('plugin/tip'); |
7 | +const stringHandle = require('common/helpers/stringHandle'); | ||
7 | 8 | ||
8 | require('common'); | 9 | require('common'); |
9 | 10 | ||
@@ -36,9 +37,9 @@ $area.on('click', function() { | @@ -36,9 +37,9 @@ $area.on('click', function() { | ||
36 | }); | 37 | }); |
37 | 38 | ||
38 | $usernameInput.bind('input propertychange', function() { | 39 | $usernameInput.bind('input propertychange', function() { |
39 | - if ($usernameInput.val().length > 20) { | 40 | + if (stringHandle.chinese2($usernameInput.val()) > 20) { |
40 | tip.show('收货人姓名最多支持20个字'); | 41 | tip.show('收货人姓名最多支持20个字'); |
41 | - $usernameInput.val($usernameInput.val() && $usernameInput.val().substring(0, 20)); | 42 | + $usernameInput.val($usernameInput.val() && stringHandle.sub_chinese2($usernameInput.val(), 20)); |
42 | } | 43 | } |
43 | }); | 44 | }); |
44 | 45 |
-
Please register or login to post a comment