12345-1234 and 1234512345112345 both work as expected
https://regex101.com/r/bDFFE9/1
Do you mean 5 to 15 digits are ok? Then you need
/(^d{5,15}$)|(^d{5}-d{4}$)/
https://regex101.com/r/bDFFE9/3/
Like
const testPhone = phoneNumber => {
if (!/(^d{5,15}$)|(^d{5}-d{4}$)/.test(phoneNumber)) {
return "PLEASE_ENTER_PHONE_NUMBER_VALID";
}
return "ok"
};
// ok:
console.log(testPhone("12345"))
console.log(testPhone("12345-1234"))
console.log(testPhone("1234512345"))
console.log(testPhone("123451234512345"))
//nok
console.log(testPhone("1234"))
console.log(testPhone("12345--1234"))
console.log(testPhone("12345123451234512345"))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…