JSFiddle - React, Tailwind, and code Playground

by Casiano Rodriguez-Leon

HTML

<p>Enter your phone number (with area code) and then click "Check".
 
 <br>The expected format is like ###-###-####.</p>
<form action="#">  
  <input id="phone">
      <button onclick="testInfo(document.getElementById('phone'));">Check
      </button>
</form>

JavaScript

var re = /\(?\d{3}\)?([-\/\.])\d{3}\1\d{4}/;  

function testInfo(phoneInput){   
    var OK = re.exec(phoneInput.value);  
    if (!OK)  
        window.alert(RegExp.input + " isn't a phone number with area code!");  
    else
        window.alert("Thanks, your phone number is " + OK[0]);  
}