判断字符串(包括汉字)的长度是否符合要求


/**
* 判断字符串(包括汉字)的长度是否符合要求
* @param str 字符串
* @param MaxLength 最大长度
* @param minLength 最小长度
* @returns  {Boolean}
* */
export default  function checkStrLength(str, MaxLength, minLength) {
  let strlength = 0; //初始定义长度为0
  var txtval = $.trim(str);
  for (let i = 0; i < txtval.length; i++) {
    let c = str.charCodeAt(i);
    //单字节加1
    if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {
      strlength++;
    } else {
      strlength += 2;
    }
    if(MaxLength && strlength  > MaxLength){
      return false;
    }
  }
  if(minLength && strlength  < minLength){
    return false;
  }
  return true;
}

你可能感兴趣的:(判断字符串(包括汉字)的长度是否符合要求)