日期:2014-05-16  浏览次数:20310 次

JS去掉空格问题
原代码是:
function   getlengthB(str)
{   return   str.replace(/[ "   "^\x00-\xff\]/g, " ").length;
}
function   Checkdata()
{
document.getElementById( "titlestr ").innerHTML= " ";
document.getElementById( "constr ").innerHTML= " ";
             
ischeck=true
                CheckUbbUse( 'UserName ',1,document.getElementById( 'Body ').value) //定员帖检查
if   (getlengthB(document.Dvform.topic.value) <3   &&   ispostnew==1)
{
document.getElementById( "titlestr ").innerHTML= "   <font   color=\ "#FF0000\ "> ←您忘记填写标题 </font> "
document.Dvform.topic.focus();
ischeck=false
}
想在getlengthB函数中增加去掉空格的命令

------解决方案--------------------
function getlengthB(str)
{
str = str.replace(/\s/g, " ");
return str.replace(/[ " "^\x00-\xff\]/g, " ").length;
}
------解决方案--------------------

function getlengthB(str)
{
return str.replace(/[ " "^\x00-\xff\]/g, " ").length;
}

String.prototype.trim = function() {
return this.replace(/(^\s*)|(\s*$)/g, " "); // 用正则表达式将前后空格
}