方法一:使用正则表达式,代码如下:
function getByteLen(val) {
var len = 0;
for (var i = 0; i < val.length; i++) {
var a = val.charAt(i);
if (a.match(/[^x00-xff]/ig) != null)
{
len += 2;
}
else
{
len += 1;
}
}
return len;
}
方法二:使用字符unicode判断:方法如下:
function getByteLen(val) {
var len = 0;
for (var i = 0; i < val.length; i++) {
var length = val.charCodeAt(i);
if(length>=0&&length<=128)
{
len += 1;
}
else
{
len += 2;
}
}
return len;
}
function queryTask(){
var taskNameQueryLength = getByteLen($("#taskNameQuery").val().trim());
if(taskNameQueryLength>4){
jAlert("任务名称长度为0-160位,其中中文算两个字符!", "系统提示");
return;
}
var scanTargetQueryLength = getByteLen($("#scanTargetQuery").val().trim());
if(scanTargetQueryLength>4){
jAlert("扫描目标长度为0-65535位!", "系统提示");
return;
}
gridQuery();
}
function getByteLen(val) {
var len = 0;
for (var i = 0; i < val.length; i++) {
var patt = new RegExp(/[^x00-xff]/ig);
var a = val[i];
if (patt.test(a))
{
len += 2;
}
else
{
len += 1;
}
}
return len;
}