RegEx for Javascript to allow only alphanumeric

前端 未结 18 1277
轻奢々
轻奢々 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:45

    JAVASCRIPT to accept only NUMBERS, ALPHABETS and SPECIAL CHARECTERS

    document.getElementById("onlynumbers").onkeypress = function (e) {
    	onlyNumbers(e.key, e)
    };
    
    document.getElementById("onlyalpha").onkeypress = function (e) {
    	onlyAlpha(e.key, e)
    };
    
    document.getElementById("speclchar").onkeypress = function (e) {
    	speclChar(e.key, e)
    };
    
    function onlyNumbers(key, e) {
    	var letters = /^[0-9]/g; //g means global
    	if (!(key).match(letters)) e.preventDefault();
    }
    
    function onlyAlpha(key, e) {
    	var letters = /^[a-z]/gi; //i means ignorecase
    	if (!(key).match(letters)) e.preventDefault();
    }
    
    function speclChar(key, e) {
    	var letters = /^[0-9a-z]/gi;
    	if ((key).match(letters)) e.preventDefault();
    }
    
       
       
          Enter Only Numbers: 
          
          

    Enter Only Alphabets:

    Enter other than Alphabets and numbers like special characters:

提交回复
热议问题