US Phone Number Format Validation in JavaScript

random

US Phone Number Format Validation in JavaScript

US Phone Number Regex JavaScript

In this blog post, I will demonstrate 10 digits US phone number validation in javascript using regular expression. Find the code snippet below :-

javascript validation for numbers,javascript validation examples,only numeric validation in javascript on keypress,numeric validation in javascript using regular expression,numeric validation in javascript for textbox,alpha numeric validation in javascript,numeric validation in javascript using keycode
10 digit phone number regex
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>us phone number format validation jquery</title>
    <script type="text/javascript">
        function ValidateUsPhoneNumber() {
            document.getElementById('txtPhone').addEventListener('input', function (e) {
                var x = e.target.value.replace(/\D/g, '').match(/(\d{0,3})(\d{0,3})(\d{0,4})/);
                e.target.value = !x[2] ? x[1] : '(' + x[1] + ') ' + x[2] + (x[3] ? '-' + x[3] : '');
            });
        }
    </script>
</head>

<body onload="ValidateUsPhoneNumber();">
    <div>
        <h1>US phone number regex javascript</h1>
        <input type="text" id="txtPhone" placeholder="(___) ___-____" />
    </div>
</body>
</html>

US Phone Number Format Validation in JavaScript Reviewed by Ravi Kumar on 11:55 PM Rating: 5

No comments:

All Rights Reserved by Etechpulse © 2012 - 2017

Contact Form

Name

Email *

Message *

Powered by Blogger.