Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Allow only alphanumeric characters in a text input ( Prevent just numbers )

I’m needing a validation function of a text input for "address" than only allows alphanumeric characters but doesn’t allow only numbers.

If the input contains only numbers can’t be submitted but if contains numbers and letters it’s okay.

Thanks in advance!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here’s what i’ve got so far;

export const getRuleOnlyAlphanumeric = () => {
  return {
    pattern: {
      value: /^[a-zA-Z0-9_.-]*$/,
      message: "Address can't contain only numbers"
    }
    
    }
  }

>Solution :

As per my understanding, the input field should have at least one alphabet, so I think the below solution could work for you. Just checking whether the string has an alphabet should suffice for the problem.

const r = /[a-zA-Z]/i


const tests = [
'dGgs1s23', // true
'12fUgdf',  // true
'121232',   // false
'abchfe',   // true
'abd()*',   // true
'42232^5$'  // false
]

tests.forEach(test => console.log(r.test(test)))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading