请将以下代码保存为一个html文件,然后用IE和火狐同时打开,看输入域获取长度是否相同。在IE和firefox两个浏览器中分别输入数据(注意换行,总共为2行):
a
b然后提交,弹出来的对话框里给出获取的长度
在IE中长度为 : 4
在fireFox中长度为: 3本来想做长度验证的,这怎么搞?长度不一样!
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>asd</title>
</head>
<body>
<textarea rows="5" cols="60" id="softDescription" name="softDescription"></textarea>
<input type="button" name="tijiao" id="tijiao" value="提交" onClick="check()">
</body>
</html>
<script type="text/javascript">
function check(){
var asd= document.getElementById("softDescription").value;
checkAllowLength(asd,1000);
}
function checkAllowLength(str,allowLength){
alert(str.replace(/[^\x00-\xff]/g,"**").length+"-------------"+allowLength+"--------"+str.length);
if(str.replace(/[^\x00-\xff]/g,"**").length>parseInt(allowLength)){
return true ;
}else{
return false;
}
}
</script>
a
b然后提交,弹出来的对话框里给出获取的长度
在IE中长度为 : 4
在fireFox中长度为: 3本来想做长度验证的,这怎么搞?长度不一样!
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>asd</title>
</head>
<body>
<textarea rows="5" cols="60" id="softDescription" name="softDescription"></textarea>
<input type="button" name="tijiao" id="tijiao" value="提交" onClick="check()">
</body>
</html>
<script type="text/javascript">
function check(){
var asd= document.getElementById("softDescription").value;
checkAllowLength(asd,1000);
}
function checkAllowLength(str,allowLength){
alert(str.replace(/[^\x00-\xff]/g,"**").length+"-------------"+allowLength+"--------"+str.length);
if(str.replace(/[^\x00-\xff]/g,"**").length>parseInt(allowLength)){
return true ;
}else{
return false;
}
}
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>asd</title>
</head>
<body>
<textarea rows="5" cols="60" id="softDescription" name="softDescription"></textarea>
<input type="button" name="tijiao" id="tijiao" value="提交" onClick="check()">
</body>
</html>
<script type="text/javascript">
function check(){
var asd= document.getElementById("softDescription").value;
asd = asd.replace('\r','') checkAllowLength(asd,1000);
}
function checkAllowLength(str,allowLength){
alert(str.replace(/[^\x00-\xff]/g,"**").length+"-------------"+allowLength+"--------"+str.length);
if(str.replace(/[^\x00-\xff]/g,"**").length>parseInt(allowLength)){
return true ;
}else{
return false;
}
}
</script>把换行符 替换了///
var asd= document.getElementById("softDescription").value;
asd = asd.replace('\r','')
checkAllowLength(asd,1000);
}
输入:
a
b
c
显示的比较的值又不一样了(只过滤了一个?)
asd = asd.replace(re,'') 试试
str.replace(/\r/g, '').replace(/[^\x00-\xff]/g,"xx");