How can i create regex for the number format 213-121-3423

Last Reply 14 days ago By dharmendr

Posted 14 days ago

How can i create regex for the number format 213-121-3423

var regex = new RegExp("\\+[0-9]{1,3}-[0-9]{3}\\-[0-9]{7}");
if (regex.test(document.getElementById("txtFax").value)) {
    alert("Valid");
} else {
    alert("invalid");
}

I have tried to above code, but its failed 

You are viewing reply posted by: dharmendr 14 days ago.
Posted 14 days ago Modified on 14 days ago

Hi azzimkazzi,

Refer below example.

HTML

<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript">
        function Validate() {
            var regex = new RegExp("^[0-9]{3}-[0-9]{3}-[0-9]{4}$");
            if (regex.test(document.getElementById("txtFax").value)) {
                alert("Valid");
            } else {
                alert("invalid");
            }
        }
    </script>
</head>
<body>
    <input type="text" id="txtFax" onchange="Validate()" />
</body>
</html>

Demo