I want to validate phone Number in the format X-XXX-XXX-XXXX
var phone= prompt("Enter phonenumber in the format X-XXX-XXX-XXXX");
var regEx= /\d-[\ddd\-]{2}\d{4}/;
if(regEx.test(phone))
{document.write("Is valid PhoneNumber "+phone);}
else{
var msg= "Chaphert 6 exmaple 2a says <br/>" +phone +" is invalid";
alert(msg);
}
I could have used regEx=/\d-\d{3}-\d{3}-\d{4}
,which will solve my problem but since I have to repeat this xxx-xxx-
,ie I don't wish to repeat \d{3}-
twice in the regex, expecting something to replace for \d{3}-\d{3}- with something like, [\d{3}-]{2}. I would appreciate any help.