Home > Enterprise >  JS - regx allow only alphanumeric
JS - regx allow only alphanumeric

Time:08-31

I'm looking for alphanumeric regex expression that allow only alphabets. I have tried with this this code

^[a-zA-Z0-9] $

But, it seem not work correctly. I'm looking for regex that allow only string below.

const str1 = 78;
const str2 = 'contains spaces';
const str3 = 'with symbols !@#$%^&';
const str4 = 'user78';
const str4 = 'khemvesna';

function alphanumeric(str) {
  const regx = /^[a-zA-Z0-9] $/;
  return regx.test(str);
}

console.log(alphanumeric(str1)); //            
  • Related