I am trying to set a phone field with following requirements:
- "+" can come if entered as first letter, otherwise not
- there can only be numbers or spaces
- there can be no more than 11 numbers
I did this which works fine for numbers and spaces but I am not sure how to restrict 11 numbers.
function validatePhone(phone) {
var filter = /^(?=.*\d)[\d ]+$/;
if (filter.test(phone)) {
return phone;
} else {
return phone.slice(0,-1);
}
Edit:
I don't want to count all of the entries as 11. I want to count only numbers and not spaces and total numbers should be 11 or 10