必须为中英文2-3个字符,自动清除首尾空格+中间空格

Posted on 2015-04-21 17:18  小蕊同学  阅读(177)  评论(0)    收藏  举报
<input type="text" id="tel">
<input type="button" value="提交" onClick="check()">
<input type="submit" value="清除首尾空格">
<script>
String.prototype.trim=function () {
return this.replace(/^\s+|\s+$/g,'');
}
function $ (id) {
return document.getElementById(id);
}
function check () {
var n=$ ('tel').value.trim ();
$ ('tel').value=n;
var p=/^([a-z]|[\u4e00-\u9fa5]){2,3}$/i;
alert(p.test(n));
}

 

<input type="button" value="提交" onClick="check()">
<input type="submit" value="清除全部空格">
<script>
String.prototype.trim=function  () {
    return this.replace(/\s/g,'');
}
function $ (id) {
    return document.getElementById(id);
}
function check () {
    var n=$ ('tel').value.trim ();
    $ ('tel').value=n;
    var p=/^([a-z]|[\u4e00-\u9fa5]){2,3}$/i;
    alert(p.test(n));
}


</script>

 

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3