Authored by 郭成尧

Merge branch 'hotfix/addressname' into 'release/5.7'

chinese-char-length2



See merge request !125
... ... @@ -9,6 +9,8 @@ var $ = require('yoho-jquery'),
area = require('./areaSelect'),
dialog = require('../../common/dialog');
var stringHandle = require('common/helpers/stringHandle');
var Dialog = dialog.Dialog,
Confirm = dialog.Confirm,
Alert = dialog.Alert;
... ... @@ -133,7 +135,7 @@ function bindOperateEvent($el) {
if (!val) {
tip = '收货人不能为空';
} else if (val.length > 20) {
} else if (stringHandle.chinese2(val) > 20) {
tip = '收货人姓名最多支持20个字';
} else if (!validateAddress({consignee: val})) {
tip = '收货人姓名不支持特殊符号';
... ...
/*
* @Author: Targaryen
* @Date: 2017-05-22 16:48:15
* @Last Modified by: Targaryen
* @Last Modified time: 2017-05-22 16:53:18
*/
let stringHandle = {
/**
* 汉字相当于两个字符的字符串长度计算
* @param {*} str
*/
chinese2(str) {
let total = 0,
charCode,
i,
len;
for (i = 0, len = str.length; i < len; i++) {
charCode = str.charCodeAt(i);
if (charCode <= 0x007f) {
total += 1;
} else {
total += 2;
}
}
return total;
},
/**
* 中文相当于两个字符情况下的字符串截取
* @param {*} str
* @param {*} n
*/
sub_chinese2(str, n) {
let r = /[\u4E00-\u9FA5]/g;
if (str.replace(r, 'mm').length <= n) {
return str;
}
let m = Math.floor(n / 2);
for (let i = m; i < str.length; i++) {
if (str.substr(0, i).replace(r, 'mm').length >= n) {
return str.substr(0, i);
}
}
return str;
}
};
module.exports = stringHandle;
... ...
... ... @@ -9,6 +9,8 @@ var regionCode = require('./common-address');
var dialog = require('../common/dialog');
var stringHandle = require('common/helpers/stringHandle');
var Alert = dialog.Alert;
var active;
... ... @@ -180,7 +182,7 @@ function blurAction(opt) {
if (opt.inputName === 'addressName') {
regular = /^([\u4e00-\u9fa5\w]{1,20})$/;
if (opt.len > 20 || opt.len < 1) {
if (stringHandle.chinese2(opt) > 20 || stringHandle.chinese2(opt) < 1) {
msg = '收货人姓名最多支持20个字';
res = false;
domClass = 'form-info form-error';
... ...
... ... @@ -9,6 +9,8 @@ var ordersApi = require('./orders-api');
var dialog = require('../../common/dialog');
var stringHandle = require('common/helpers/stringHandle');
var Dialog = dialog.Dialog;
var Confirm = dialog.Confirm;
... ... @@ -318,7 +320,7 @@ function validateForm() {
return {
name: function(name) {
if (name.length > 20) {
if (stringHandle.chinese2(name) > 20) {
$errName.html(_errorHtml);
$errName.find('b').html('收货人姓名最多支持20个字');
$errName.show();
... ...
... ... @@ -7,6 +7,8 @@ var $ = require('yoho-jquery'),
Addr = require('./common-address'),
_dialog = require('../common/dialog');
var stringHandle = require('common/helpers/stringHandle');
var $goodsTable = $('#goods-table'),
$checkBox = $goodsTable.find('input[type="checkbox"]'),
$reasons = $goodsTable.find('.return-reason'),
... ... @@ -483,7 +485,7 @@ function packExchangeInfo() {
if (!verifyTip) {
if (!res.consigneeName) {
verifyTip = '请填写收货人姓名';
} else if (res.consigneeName.length > 20) {
} else if (stringHandle.chinese2(res.consigneeName) > 20) {
verifyTip = '收货人姓名最多支持20个字';
} else if (!nameReg.test(res.consigneeName)) {
verifyTip = '收货人姓名不支持特殊符号';
... ...