Wed, April 14, 2021
 
https://devcodepro.comdevcodepro
Home · Latest · Trends  /  JavaScript
2

Validating international phone numbers

Validating international phone numbers with jquery
<!DOCTYPE html>
<html> 
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<input type="text" id="number" name="phonenumber" />
<script>
jQuery('#number').keyup(function () { 
this.value = this.value.replace(/[^0-9.s+()]/g,'');
});
</script>
</body>
</html>
rated 2 times (2) (0)
comments: 1 / hits: 2374  / 5 years ago, sun, sep 25, 16, 02:52:42
More From » JavaScript
 

Comments

3
You may use this..

Code:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<input type="text" id="number" name="phonenumber" />
<script>
jQuery('#number').keypress(function(e) {
var numbersTyped = String.fromCharCode(e.which);
if (/[^0-9.s+()]/i.test(numbersTyped)) {
alert("This is not valid phone number. Please enter the phone number in format +12144454567");
$("#number").val('');
return false;
}
if($(this).val().length >= 12) {
$(this).val($(this).val().slice(11, 12));
alert("This is not valid phone number. Please enter the phone number in format +12144454567");
$("#number").val('');
return false;
}
});
jQuery('.numbersOnly').keyup(function () {
this.value = this.value.replace(/[^0-9\.]/g,'');
});
</script>
</body>
</html>
[#1] Sunday, September 25, 2016, 4:14:56
 
James
commented 5 years ago
Only authorized users can post. Please sign in first, or register a free account
 
Share
Posted
Ronald
Member since Sep 6, 2016
Total Code Snippets: 23
Total Comments: 1
Location: New Britain, Connecticut
Ronald snippets
5 years ago, tue, sep 6, 16, 10:11:38
comments: 0 / hits: 2328
PHP
5 years ago, tue, sep 6, 16, 10:18:23
comments: 0 / hits: 2235
PHP
5 years ago, tue, sep 6, 16, 10:42:24
comments: 0 / hits: 2527
PHP
5 years ago, fri, sep 16, 16, 1:10:12
comments: 0 / hits: 2801
PHP
5 years ago, fri, sep 16, 16, 1:12:32
comments: 0 / hits: 2021
CSS
5 years ago, fri, sep 16, 16, 1:27:33
comments: 0 / hits: 3003
PHP
5 years ago, thu, nov 10, 16, 4:21:59
comments: 1 / hits: 2479
PHP
4 years ago, mon, nov 21, 16, 10:48:23
comments: 2 / hits: 7777
PHP
4 years ago, sun, dec 25, 16, 2:44:24
comments: 0 / hits: 4601