JavaScript, regular expression to validate phone numbers
<p>I want to validate phone Number in the format X-XXX-XXX-XXXX</p>

<pre><code>    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 &lt;br/&gt;" +phone +" is invalid";
     alert(msg);
    }
</code></pre>

<p>I could have used <code>regEx=/\d-\d{3}-\d{3}-\d{4}</code>,which will solve my problem but since I have to repeat this <code>xxx-xxx-</code>,ie I don't wish to repeat <code>\d{3}-</code> twice in the regex, expecting something to replace for \d{3}-\d{3}- with something like, [\d{3}-]{2}. I would appreciate any help.</p>