input phone number code example
Example 1: how to validate mobile number in html form
<div>
<label for="phone">Enter your phone number:</label>
<input type="tel" id="phone" name="phone" pattern="+94[7-9]{2}-[0-9]{3}-[0-9]{4}" value="+94">
</div>
Example 2: input type for contact no
label for="phone">Enter your phone number:</label>
<input type="tel" id="phone" name="phone" pattern="[0-9]{3}-[0-9]{2}-[0-9]{3}">
Example 3: phone number input
JS Libraries required :
======================
https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js
https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/8.4.7/js/intlTelInput.js
Inside JS File :
================
$("input").intlTelInput({
utilsScript: "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/8.4.6/js/utils.js"
});
Inside HTML File :
===================
<h1>International Telephone Input - BOOTSTRAP INPUT GROUP</h1>
<form>
<div class="input-group">
<input type="tel" class="form-control">
<span class="input-group-addon">Tel</span>
</div>
<br>
<div class="input-group">
<input type="tel" class="form-control">
<span class="input-group-addon">Tel</span>
</div>
</form>
Inside Css File :
=================
body {
margin: 20px;
}
form {
width: 300px;
}
// workaround
.intl-tel-input {
display: table-cell;
}
.intl-tel-input .selected-flag {
z-index: 4;
}
.intl-tel-input .country-list {
z-index: 5;
}
.input-group .intl-tel-input .form-control {
border-top-left-radius: 4px;
border-top-right-radius: 0;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 0;
}
Example 4: phone number input with country code in html
//Author: Mohammad Arman Khan
<input name="phone" type="text" class="form-control mb-2 inptFielsd" id="phone"
placeholder="Phone Number" />
<script>
var input = document.querySelector("#phone");
window.intlTelInput(input, {
separateDialCode: true,
customPlaceholder: function (
selectedCountryPlaceholder,
selectedCountryData
) {
return "e.g. " + selectedCountryPlaceholder;
},
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/js/intlTelInput.min.js"
integrity="sha512-DNeDhsl+FWnx5B1EQzsayHMyP6Xl/Mg+vcnFPXGNjUZrW28hQaa1+A4qL9M+AiOMmkAhKAWYHh1a+t6qxthzUw=="
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/css/intlTelInput.min.css"
integrity="sha512-yye/u0ehQsrVrfSd6biT17t39Rg9kNc+vENcCXZuMz2a+LWFGvXUnYuWUW6pbfYj1jcBb/C39UZw2ciQvwDDvg=="
crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/js/utils.js"
integrity="sha512-BNZ1x39RMH+UYylOW419beaGO0wqdSkO7pi1rYDYco9OL3uvXaC/GTqA5O4CVK2j4K9ZkoDNSSHVkEQKkgwdiw=="
crossorigin="anonymous"></script>
Example 5: javascript phone number input
function phonenumber(inputtxt)
{
var phoneno = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
if((inputtxt.value.match(phoneno))
{
return true;
}
else
{
alert("message");
return false;
}
}