js屏蔽全角空格

 

function msgSubmit(){
    if($("#email").val()==""){
        alert('会员邮箱不得为空!');
        return false;
    }
    var patrn = /^([a-z0-9A-Z]+[-|/.|_]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?/.)+[a-zA-Z]{2,}$/;
    if (!patrn.test($("#email").val())){
        alert('邮箱格式不正确!');
        return false;
    }
   var cont = $("#content").val();
    if(cont=="" || cont.trim()==""|| cont.trim()=="必填"){
        alert('留言内容不得为空!');
        return false;
   }
    if($("#content").val().length>300){
            alert('留言内容不得超过300字!');
            return false;
    }
    return true;
}

 

String.prototype.trim = function()    
{    
     // 用正则表达式将前后空格 用空字符串替代。     
     var t = this.replace(/(^/s*)|(/s*$)/g, "");  
     return t.replace(/(^ *)|( *$)/g, ""); 
 
}

 

首先是把/(^/s*)|(/s*$)/g 替换为""
然后,/.../g 里面的,是表示放置通配符的地方,g代表全局参数,(^/s*)或者(/s*$)都将被替换为"" 


匹配首尾空白字符的正则表达式:^/s*|/s*$ 可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式

你可能感兴趣的:(正则表达式,function)