JS过滤空白字符

function Trim(str) 
{  
  return str.replace(/^\s+|\s+$/g, ""); //去除字符串两边的空白符
    //return this.replace(/^\s+/g,"").replace(/\s$+/g,"");//去除字符串两边的空白符
    //return this.replace(/^\s+/g,"");//去除字符串左边的空白符
    //return this.replace(/\s$+/g,"");//去除字符串右边边的空白符
}

html中文本框只能输入中文英文数字下划线组合:
function checkInput()
{
    var str=document.getElementById("name").value;
    if(/^[\u4e00-\u9fa5_a-zA-Z0-9]+$/.test(str)){
        alert("输入ok");
    }else{
        alert("输入error");
    }
}

你可能感兴趣的:(JS过滤空白字符)