RegEx for Javascript to allow only alphanumeric

前端 未结 18 1253
轻奢々
轻奢々 2020-11-22 15:13

I need to find a reg ex that only allows alphanumeric. So far, everyone I try only works if the string is alphanumeric, meaning contains both a letter and a number. I just w

相关标签:
18条回答
  • 2020-11-22 15:20

    Extend the string prototype to use throughout your project

        String.prototype.alphaNumeric = function() {
            return this.replace(/[^a-z0-9]/gi,'');
        }
    

    Usage:

        "I don't know what to say?".alphaNumeric();
        //Idontknowwhattosay
    
    0 讨论(0)
  • 2020-11-22 15:24
    /^([a-zA-Z0-9 _-]+)$/
    

    the above regex allows spaces in side a string and restrict special characters.It Only allows a-z, A-Z, 0-9, Space, Underscore and dash.

    0 讨论(0)
  • 2020-11-22 15:24

    It seems like many users have noticed this these regular expressions will almost certainly fail unless we are strictly working in English. But I think there is an easy way forward that would not be so limited.

    1. make a copy of your string in all UPPERCASE
    2. make a second copy in all lowercase

    Any characters that match in those strings are definitely not alphabetic in nature.

    let copy1 = originalString.toUpperCase();
    let copy2 = originalString.toLowerCase();
    for(let i=0; i<originalString.length; i++) {
        let bIsAlphabetic = (copy1[i] != copy2[i]);
    }
    

    Optionally, you can also detect numerics by just looking for digits 0 to 9.

    0 讨论(0)
  • 2020-11-22 15:24
    Question is old, but it's never too late to answer
    
    $(document).ready(function() {
      //prevent paste
      var usern_paste = document.getElementById('yourid');
      usern_paste.onpaste = e => e.preventDefault();
    
      //prevent copy
      var usern_drop = document.getElementById('yourid');
      usern_drop.ondrop = e => e.preventDefault();
    });
    
    $('#yourid').keypress(function (e) {
      var regex = new RegExp("^[a-zA-Z0-9\s]");
      var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
      if (regex.test(str)) {
          return true;
      }
      e.preventDefault();
      return false;
    });
    
    0 讨论(0)
  • 2020-11-22 15:26

    Jquery to accept only NUMBERS, ALPHABETS and SPECIAL CHARECTERS

    <html>
    <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    </head>
    
    <body>
    Enter Only Numbers: 
    <input type="text" id="onlynumbers">
    <br><br>
    Enter Only Alphabets: 
    <input type="text" id="onlyalpha">
    <br><br>
    Enter other than Alphabets and numbers like special characters: 
    <input type="text" id="speclchar">
    
    <script>
        $('#onlynumbers').keypress(function(e) {
          var letters=/^[0-9]/g; //g means global
          if(!(e.key).match(letters)) e.preventDefault();
    	});
        
        $('#onlyalpha').keypress(function(e) {
          var letters=/^[a-z]/gi; //i means ignorecase
          if(!(e.key).match(letters)) e.preventDefault();
    	});
        
        $('#speclchar').keypress(function(e) {
          var letters=/^[0-9a-z]/gi; 
          if((e.key).match(letters)) e.preventDefault();
    	});
        </script>
    </body>
    </html>

    **JQUERY to accept only NUMBERS , ALPHABETS and SPECIAL CHARACTERS **
    
    
    <!DOCTYPE html>
        $('#onlynumbers').keypress(function(e) {
          var letters=/^[0-9]/g; //g means global
          if(!(e.key).match(letters)) e.preventDefault();
        });
    
        $('#onlyalpha').keypress(function(e) {
          var letters=/^[a-z]/gi; //i means ignorecase
          if(!(e.key).match(letters)) e.preventDefault();
        });
    
        $('#speclchar').keypress(function(e) {
          var letters=/^[0-9a-z]/gi; 
          if((e.key).match(letters)) e.preventDefault();
        });
    
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> 
    

    Enter Only Numbers:

    Enter Only Alphabets:

    Enter other than Alphabets and numbers like special characters:

    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-22 15:28

    Alphanumeric with case sensitive:

    if (/^[a-zA-Z0-9]+$/.test("SoS007")) {
      alert("match")
    }
    
    0 讨论(0)
提交回复
热议问题