The phone number I need to validate needs to be at least 8 characters long and has two possible formats:
Must be at least 8 digits like "12345678"
OR
Must have 2 or 3 digits before "-" and 6 or more digits after "-"
I need to solve this using a regex.
I have tried this but it doesn't seem to work properly (1-22334455) gets accepted even though I want it to be rejected.
number: {
type: String,
minLength: 8,
validate: {
validator: v => /\d{2,3}-\d{6,}/.test(v) || /\d{8,}/.test(v),
message: "this is not a valid number"
}
}
Another attempt (still doesn't quite do it):
number: {
type: String,
minLength: 8,
validate: {
validator: v => /\d{2,3}-?\d{6,}/.test(v),
message: "this is not a valid number"
}
}
How can I improve it?