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!
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"
}
}
}
CodePudding user response:
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)))
CodePudding user response:
This should work
const r = /^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9] )$/i
const tests = [
'dGgs1s23', // true
'12fUgdf' , // true
'121232' , // false
'abchfe' , // false
'abd()*' , // false
'42232^5$' // false
]
tests.forEach(test => console.log(r.test(test)))