天天看点

js正则只能输入中文

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>js正则匹配中文标点符号</title>
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<head>
<body>
<input type="text" id="textBox" maxlength="1" /> 
<input type="button" onclick="checkText()" value="提交" />

<br/>
<br/><br/><br/><br/><br/>
<form method="post" onsubmit="subname()">
        <input type="text" id="input" value="" />
        <button type="submit" id="btn">提交</button>
    </form>

<input type="text" id="textBox2"  style="margin-top:20px;"/> 
<input type="button" onclick="checkText2()" value="提交" />

<script type="text/javascript">
function checkText()
{
 var text = document.getElementById('textBox').value;
 //匹配这些中文标点符号 。 ? ! , 、 ; : “ ” ‘ ' ( ) 《 》 〈 〉 【 】 『 』 「 」 ﹃ ﹄ 〔 〕 … — ~ ﹏ ¥
 var reg = /[\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b|\uff1a|\u201c|\u201d|\u2018|\u2019|\uff08|\uff09|\u300a|\u300b|\u3008|\u3009|\u3010|\u3011|\u300e|\u300f|\u300c|\u300d|\ufe43|\ufe44|\u3014|\u3015|\u2026|\u2014|\uff5e|\ufe4f|\uffe5]/;
 if(reg.test(text)){
  alert('是中文标点符号');
 }else{
  alert('不是中文标点符号');
 }
}

    function subname() {
    debugger;
        var str = $("#input").val();
        var han = /^[\u4e00-\u9fa5]+$/;
        if (str == '') {
            alert("请输入汉字");
            return false;
        };
        if (!han.test(str)) {
            alert("不是汉字")
            return false;
        };
         alert("是汉字")
        return true;
    };

function checkText2()
{
 var text = document.getElementById('textBox2').value;
 //匹配只包含中文和、和A-Z和0-9 \u4e00-\u9fa5表示中文正则,\u3001表示、正则;\d表示数字 。 以中文或A-Z或0-9或、开头都可以。
 var reg = /^[\u4e00-\u9fa5\u3001\A-\Z\d]+$/;
 if(reg.test(text)){
  alert('是只包含中文和、');
 }else{
  alert('false不只包含中文和、');
 }
}   
</script>
</body>
</html>
           
js正则只能输入中文
js正则只能输入中文