I need to validate an account number.
A valid number could be either a sequence of exactly 11 digits or 3 groups of digits separated by hyphens (2 digits - 3 digits - 6 digits)
I tried this :
/^([0-9]{11})|([0-9]{2}-[0-9]{3}-[0-9]{6})$/
But it only works for the second rule . The first rule doesn't work as it allows numbers of more than 11 digits
This is how I use the regex in my js function :
var re = /^([0-9]{11})|([0-9]{2}-[0-9]{3}-[0-9]{6})$/;
if (re.test(txtNumber.value)==true) {
return 1;
}
else {
alert(Invalid Account Number);
return 0;
}
Any advise or guidance would be greatly appreciated
VALID NUMBERS:
12345678912 (11 digits)
12-345-678912 (11 digits separated by hyphens)
INVALID NUMBERS:
1223 (less than 11 digits)
111111111111 ( more than 11 digits)
123-23-678912 (11 digits , but not separated correctly, it should be 2 digits-3 digits-6 digits)