input 輸入框正則限制只能輸入字母,數字,漢字 原理解析以及例子

 

 

原理 : 利用監聽鍵盤擡起的事件,然後事件中過濾一下對應的不符合規定的字符

 

正則表達式教程 : 正則表達式詳解[從入門到精通]

<input type="text" onkeyup="this.value=this.value.replace(/[90]/,'')">

//    其中/[90]/ 表示匹配9或者0
//    這個input 不能接收9或者0的輸入
//    {this.value=this.value.replace(/[90]/,''} 的作用是將字符串中遇到的一個9和0替換爲''
<input type="text" onkeyup="this.value=this.value.replace(/[90]/g,'')">

//    這個和上一個的區別是 正則之後加了一個g 意思是所有的9和0替換成''
//    想實驗這個效果可以在輸入框長按9或者0觀察現象
//    也可以將12345012391284023490343400 複製到兩個輸入框觀察現象
<input type="text" onkeyup="this.value=this.value.replace(/[^(90)]/g,'')">

//    這個和上面兩個的區別是隻接受9和0 會把另外的元素都替換爲'' 
//    ^ 是取反的意思

 

 

So ... 知道了原理,就能擼出一些東西來了[手動滑稽]

 

比如 : 


    

    <!-- 只能輸入數字 -->
    <!-- 數字正則爲[\d] -->
    <input type="text" onkeyup="this.value=this.value.replace(/[^\d]/g,'')" placeholder="純數字">

    <!-- 只能輸入字母 -->
    <!-- 字母正則爲[a-zA-Z] -->
    <input type="text" onkeyup="this.value=this.value.replace(/[^a-zA-Z]/g,'')" placeholder="純字母">

    <!-- 只能輸入數字字母下劃線 -->
    <!-- 正則爲[\w] -->
    <input type="text" onkeyup="this.value=this.value.replace(/[^\w]/g,'')" placeholder="數字字母下劃線">

    <!-- 只能輸入漢字 -->
    <!-- [\u4e00-\u9fa5] -->
    <input type="text" onkeyup="this.value=this.value.replace(/[^\u4e00-\u9fa5]/g,'')" placeholder="漢字">

    <!-- 只能輸入上面所有的 [數字,字母,下劃線,或者漢字] -->
    <!-- [\w\u4e00-\u9fa5] -->
    <input type="text" onkeyup="this.value=this.value.replace(/[^\w\u4e00-\u9fa5]/g,'')" placeholder="數字,字母,下劃線,或者漢字">

 

當然 可以讓onkeyup指向一個function 做更深層次的操作

 

- 拼接FUCK例子


   

    <!-- 拼接FUCK例子 -->
    <input type="text" onkeyup="this.value=fuckText(this.value)" placeholder="FuckRedWolfChao">

   <script>
       function fuckText(value){
            //    [代碼水平不咋地,只是個例子]...
            //    當你輸入RedWolfChao的時候拼接FUCK
            if(value == "RedWolfChao"){
                return "FUCK : " + value;
            }
            return value;
        }
    </script>

 

- 正則校驗合法金錢例子


    

    <!-- 正則校驗合法金錢例子 -->

    <input type="text" onkeyup="this.value=fuckMoney(this.value)" placeholder="fuckMoney">

    <script>
        function fuckMoney(value){
            //    [代碼水平不咋地,只是個例子]
            //    input 正則校驗合法金錢數目
            //    1. 不能以0開頭
            //    2. 不能以小數點開頭
            //    3. 只能同時存在一個小數點
            //    然後就寫邏輯唄
            //    正則控制只能輸入數字和小數點
            var temp = value.replace(/[^\d\.]/g,'');
            //    然後判斷第一位是不是0或者小數點 如果是 就return ''
            var zeroIndex = temp.indexOf('0');
            if (zeroIndex==0) {
                return'';
            }
            var pointIndex = temp.indexOf('.');
            if (pointIndex==0) {
                return '';
            }else if (pointIndex!=-1) {
                //    正則判斷 包含幾個小數點
                var reg = /\./g;
                var pointNum = temp.match(reg).length;

                //    如果有兩個 則處理一下 否則就幹掉了
                if(pointNum==2){
                    //    如果第二個小數點是最後一個,則幹掉它,否則清空Input
                    var lastPointIndex = temp.lastIndexOf('.');
                    if (lastPointIndex+1 == temp.length) {
                        return temp.substring(0,temp.length-1);
                    }
                }else if(pointNum ==1 ){
                    return temp;
                }else{
                    return '';
                }
            }
            return temp;
        }
    </script>

 

最後轉載一個同道寫好的正則匹配[我還是推薦自己寫,可以寫好之後和他的對比一下有什麼不同]

 

可以直接去該網站看,也可以往下翻 [代碼後面還有...還得繼續往下翻]

直達鏈接
 

輸入大小寫字母、數字、下劃線:

<input type="text" onkeyup="this.value=this.value.replace(/[^\w_]/g,'');">

輸入小寫字母、數字、下劃線:

<input type="text" onkeyup="this.value=this.value.replace(/[^a-z0-9_]/g,'');">

輸入數字和點

<input type="text" onkeyup="value=value.replace(/[^\d.]/g,'')">

輸入中文:   

<input type="text" onkeyup="this.value=this.value.replace(/[^\u4e00-\u9fa5]/g,'')">  
  
輸入數字:   

<input type="text" onkeyup="this.value=this.value.replace(/\D/g,'')">  

輸入英文:   

<input type="text" onkeyup="this.value=this.value.replace(/[^a-zA-Z]/g,'')">  
  
輸入中文、數字、英文:   

<input onkeyup="value=value.replace(/[^\w\u4E00-\u9FA5]/g, '')">  
 
輸入數字和字母:

<input onKeyUp="value=value.replace(/[\W]/g,'')">  

除了英文的標點符號以外,其他的都可以中文,英文字母,數字,中文標點

<input type="text" onkeyup="this.value=this.value.replace(/^[^!@#$%^&*()-=+]/g,'')">

只能輸入數字代碼(小數點也不能輸入)

<input onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">

只能輸入數字,能輸小數點.

<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')">

<input name=txt1 onchange="if(/\D/.test(this.value)){alert('只能輸入數字');this.value='';}">

數字和小數點方法二

<input type=text t_value="" o_value="" onkeypress="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onkeyup="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onblur="if(!this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/))this.value=this.o_value;else{if(this.value.match(/^\.\d+$/))this.value=0+this.value;if(this.value.match(/^\.$/))this.value=0;this.o_value=this.value}">

只能輸入字母和漢字

<input onkeyup="value=value.replace(/[\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[\d]/g,''))" maxlength=10 name="Numbers">

只能輸入英文字母和數字,不能輸入中文

<input onkeyup="value=value.replace(/[^\w\.\/]/ig,'')">

只能輸入數字和英文

<input onKeyUp="value=value.replace(/[^\d|chun]/g,'')">

小數點後只能有最多兩位(數字,中文都可輸入),不能輸入字母和運算符號:

<input onKeyPress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || /\.\d\d$/.test(value))event.returnValue=false">

小數點後只能有最多兩位(數字,字母,中文都可輸入),可以輸入運算符號:

<input onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')">

 

前方高能!

前方高能!!

前方高能!!!

這種正則方式攔截不住輸入法Enter輸入

 

原因 : 第三方輸入法攔截了Enter的事件,瀏覽器監聽不到...

解決方式 : 將onkeyup改爲onInput即可...

實驗證明 : oninput體驗比onkeyup更舒適

 

    <!-- oninput 事件在用戶輸入時觸發。-->
    <!-- 該事件在 或 元素的值發生改變時觸發。-->
    <!-- 提示: 該事件類似於 onchange 事件。不同之處在於 oninput 事件在元素值發生變化是立即觸發, onchange 在元素失去焦點時觸發。另外一點不同是 onchange 事件也可以作用於 和 元素。-->

    <!-- 只能輸入數字 -->
    <!-- 數字正則爲[\d] -->
    <input type="text" oninput="this.value=this.value.replace(/[^\d]/g,'')" placeholder="純數字">

 

最終代碼 長這樣 : 

 

<!DOCTYPE html>
<html>
<meta charset="UTF-8"> 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
	<title>Input</title>
</head>
<body>
	<!-- 只能輸入數字 -->
	<!-- 數字正則爲[\d] -->
	<input type="text" oninput="this.value=this.value.replace(/[^\d]/g,'')" placeholder="純數字">
	<!-- 只能輸入字母 -->
	<!-- 字母正則爲[a-zA-Z] -->
	<input type="text" oninput="this.value=this.value.replace(/[^a-zA-Z]/g,'')" placeholder="純字母">
	<!-- 只能輸入數字字母下劃線 -->
	<!-- 正則爲[\w] -->
	<input type="text" oninput="this.value=this.value.replace(/[^\w]/g,'')" placeholder="數字字母下劃線">
	<!-- 只能輸入漢字 -->
	<!-- [\u4e00-\u9fa5] -->
	<input type="text" oninput="this.value=this.value.replace(/[^\u4e00-\u9fa5]/g,'')" placeholder="漢字">
	<!-- 只能輸入上面所有的 [數字,字母,下劃線,或者漢字] -->
	<!-- [\w\u4e00-\u9fa5] -->
	<input type="text" oninput="this.value=this.value.replace(/[^\w\u4e00-\u9fa5]/g,'')" placeholder="數字,字母,下劃線,或者漢字">
	<!-- 拼接FUCK例子 -->
	<input type="text" oninput="this.value=fuckText(this.value)" placeholder="FuckRedWolfChao">
	<!-- 正則校驗合法金錢例子 -->
	<input type="text" oninput="this.value=fuckMoney(this.value)" placeholder="fuckMoney">
	<script>
		function fuckText(value){
			//	只是個例子...
			//	當你輸入RedWolfChao的時候拼接FUCK
			if(value == "RedWolfChao"){
				return "FUCK : " + value;
			}
			return value;
		}
		function fuckMoney(value){
			//	input 正則校驗合法金錢數目
			//	1. 不能以0開頭
			//	2. 不能以小數點開頭
			//	3. 只能同時存在一個小數點
			//	然後就寫邏輯唄
			//	正則控制只能輸入數字和小數點
			var temp = value.replace(/[^\d\.]/g,'');
			//	然後判斷第一位是不是0或者小數點 如果是 就return ''
			var zeroIndex = temp.indexOf('0');
			if (zeroIndex==0) {
				return'';
			}
			var pointIndex = temp.indexOf('.');
			if (pointIndex==0) {
				return '';
			}else if (pointIndex!=-1) {
				//	正則判斷 包含幾個小數點
				var reg = /\./g;
				var pointNum = temp.match(reg).length;
				//	如果有兩個 則處理一下 否則就幹掉了
				if(pointNum==2){
					//	如果第二個小數點是最後一個,則幹掉它,否則清空Input
					var lastPointIndex = temp.lastIndexOf('.');
					if (lastPointIndex+1 == temp.length) {
						return temp.substring(0,temp.length-1);
					}
				}else if(pointNum ==1 ){
					return temp;
				}else{
					return '';
				}
			}
			return temp;
		}
	</script>
</body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章