RegEx with valid string of characters

by jacobwsmith

JavaScript

console.clear();
function test(value) {
  let isValid = true;
  const validChars = 'abcdefghijklmnopqrstuvwxyz0123456789';
  const re = new RegExp('^['+validChars+']*$');
  value = value.trim();
  if (value !== '') {
    isValid = re.test(value);
  }
  return isValid; // ? null : { invalidChars: true };
}

console.log('valid: ', test('123abc'));
console.log('valid: ', test('123abC'));
console.log('valid: ', test('123ab~'));