javascript驗證漢字,字元,等Regex

來源:互聯網
上載者:User

驗證使用者名稱的一個例子:

 

html:

 <input type="text" value="" name="username" id="username" onblur="checkUser()"/><span id="usertips"></span>

 

js:

function checkUser() 

{

var username = $("#username").val();

if (!username.match( /^[\u4E00-\u9FA5a-zA-Z0-9_]{3,20}$/)) {
var msg = "漢字 英文字母 數字 底線組成,3-20位";
$("#usertips").html(msg);
} else {
 $("#usertips").html('');
}
}

1、只允許輸入數字 
    <input name="username" type="text" onkeyup="value=this.value.replace(/\D+/g,’’)"> 

    2、只允許輸入英文字母、數字和底線(以下二種方法實現) 
    <input name="username" type="text" style="ime-mode:disabled"> 
    <input name="username" type="text" onkeyup="value=value.replace(/[^\w\.\/]/ig,’’)"> 

    3、只允許輸入英文字母、數字和=@# 
    <input name="username" type="text" onkeyup="value=value.replace(/[^\w=@#]|_/ig,’’)"> 

    4、只允許輸入漢字 
    <input name="username" type="text" onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,’’)"> 
1、只允許輸入數字 
    <input name="username" type="text" onkeyup="value=this.value.replace(/\D+/g,’’)"> 

    2、只允許輸入英文字母、數字和底線(以下二種方法實現) 
    <input name="username" type="text" style="ime-mode:disabled"> 
    <input name="username" type="text" onkeyup="value=value.replace(/[^\w\.\/]/ig,’’)"> 

    3、只允許輸入英文字母、數字和=@# 
    <input name="username" type="text" onkeyup="value=value.replace(/[^\w=@#]|_/ig,’’)"> 

    4、只允許輸入漢字 
    <input name="username" type="text" onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,’’)"> 

只能輸入數字:"^[0-9]*$"。 
只能輸入n位的數字:"^\d{n}$"。 
只能輸入至少n位的數字:"^\d{n,}$"。 
只能輸入m~n位的數字:。"^\d{m,n}$" 
只能輸入零和非零開頭的數字:"^(0|[1-9][0-9]*)$"。 
只能輸入有兩位小數的正實數:"^[0-9]+(.[0-9]{2})?$"。 
只能輸入有1~3位小數的正實數:"^[0-9]+(.[0-9]{1,3})?$"。 
只能輸入非零的正整數:"^\+?[1-9][0-9]*$"。 
只能輸入非零的負整數:"^\-[1-9][]0-9"*$。 
只能輸入長度為3的字元:"^.{3}$"。 
只能輸入由26個英文字母組成的字串:"^[A-Za-z]+$"。 
只能輸入由26個大寫英文字母組成的字串:"^[A-Z]+$"。 
只能輸入由26個小寫英文字母組成的字串:"^[a-z]+$"。 
只能輸入由數字和26個英文字母組成的字串:"^[A-Za-z0-9]+$"。 
只能輸入由數字、26個英文字母或者底線組成的字串:"^\w+$"。 
驗證使用者密碼:"^[a-zA-Z]\w{5,17}$"正確格式為:以字母開頭,長度在6~18之間,只能包含字元、數字和底線。 
驗證是否含有^%&',;=?$\"等字元:"[^%&',;=?$\x22]+"。 

只能輸入漢字:"^[\u4e00-\u9fa5]{0,}$"

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.