JS 간단하고 실용적인 작은 기능 (1)

13848 단어
1. 문자열 양쪽 공백 필터
 
1 <input type='text' id='strs' value='         '>
2 <input type='button' id='rstrsBtn' value='   '>

 
    window.onload = function(){
        var _rstrsBtn = document.getElementById("rstrsBtn"),            // 
                _strs = document.getElementById("strs");            // 

        _rstrsBtn. onclick = function(){// 
            _strs.value = _strs.value.replace( /^(\s|\u00A0)+|(\s|\u00A0)+$/g, "" );// 
        }
    };

2. 입력 여부 확인
<input type='text' id='strs' value='         '>
<input type='button' id='isContent' value=' '><br>
 1    window.onload = function(){
 2         var _isContent = document.getElementById("isContent"),
 3                 _strs = document.getElementById("strs");
 4         _isContent.onclick = function(){
 5             if(!_strs.value.replace( /^(\s|\u00A0)+|(\s|\u00A0)+$/g, "" )){
 6                 alert(" !");
 7             }else{
 8                 alert(" !");
 9             }
10         }
11     };

3. 복사 및 붙여넣기 금지
1 <input type="text" name="banCopyPaste"  id='banCopyPaste'/><br>
1     var banCopyPaste = document.getElementById("banCopyPaste");
2     banCopyPaste.oncopy = function(){        // 
3         return false;
4     }
5     banCopyPaste.onpaste = function(){        // 
6         return false;
7     }

4. 입력 제한(숫자)
1 <form>
2     <input type="number" name="numberHtml5"  value='html5 ' id='banNumberHtml5'/><br>
3     <input type="text" value='html5 '  pattern=[0-9] id='patternHtml5'/>
4     <input type="text" name="number"  value=' ' id='banNumber'/><br>
5     <input type='submit' value=' '>
6 form>
 1     window.onload = function(){
 2         var banNumber = document.getElementById("banNumber"),
 3                 clearNonumber = function(tThis){        // 
 4                     var _v = tThis.value;
 5                     tThis.value = _v.replace(/\D/g,"");
 6                 }
 7         banNumber.onfocus = function(){            // 
 8             clearNonumber(this);
 9         }
10         banNumber.onkeyup = function(){            // 
11             clearNonumber(this);
12         }
13         banNumber.onblur = function(){                // 
14             clearNonumber(this);
15         }
16     };

5. 입력 제한(중국어)
1 <input type="text" name="number"  value=' ' id='chineseStr'/><br>
 1     window.onload = function(){
 2         var chineseStr = document.getElementById("chineseStr"),
 3                 clearNonumber = function(tThis){                    // 
 4                     var _v = tThis.value;
 5                     tThis.value = _v.replace(/[^\u4e00-\u9fa5]/g,"");    // 
 6                 }
 7         chineseStr.onfocus = function(){                            // 
 8             clearNonumber(this);
 9         }
10 
11         chineseStr.onkeyup = function(){                            // 
12             clearNonumber(this);
13         }
14         chineseStr.onblur = function(){                            // 
15             clearNonumber(this);
16         }
17     };

 
전재 대상:https://www.cnblogs.com/HopeInTheDark/p/11433672.html

좋은 웹페이지 즐겨찾기