只允许输入数字
<
input
name
=
"username"
type
=
"text"
onkeyup
=
"value=this.value.replace(/\D+/g,'')"
>
只允许输入英文字母、数字和下划线(以下二种方法实现)
<
input
name
=
"username"
type
=
"text"
style
=
"ime-mode:disabled"
>
<
input
name
=
"username"
type
=
"text"
onkeyup
=
"value=value.replace(/[^\w\.\/]/ig,'')"
>
只允许输入英文字母、数字和=@#
<
input
name
=
"username"
type
=
"text"
onkeyup
=
"value=value.replace(/[^\w=@#]|_/ig,'')"
>
只允许输入汉字
<
input
name
=
"username"
type
=
"text"
onkeyup
=
"value=value.replace(/[^\u4E00-\u9FA5]/g,'')"
>
验证中文姓名(包括如:贾格思·比伯 等):
正则:/^[\u4E00-\u9FA5·s]{2,6}$/
var chk = /^[\u4E00-\u9FA5·s]{2,6}$/; if(!chk.test(name)){ alert("正确"); }