How to allow only phone number in input and make it validate

Posted by Manideepgoud under jQuery category on | Points: 40 | Views : 1449
In the below code, am showing how to allow only numbers without text and get alert if we give more than 10 numbers?
<!DOCTYPE html>
<html lang="en">
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="">
<script src=""></script>
<script src=""></script>
<script src=""></script>
<script type="text/javascript">
function validatephone(phone)
var maintainplus = '';
var numval = phone.value;
if(phone.value.length > 10){
alert("Please enter the valid Phone number")

if ( numval.charAt(0)=='+' )
var maintainplus = '';
curphonevar = numval.replace(/[\\A-Za-z!"£$%^&\,()*+_={};:'@#~,.Š\/<>?|`¬\]\[]/g,'');
phone.value = maintainplus + curphonevar;
var maintainplus = '';

<div class="container">
<label>Phone Number:</label>
<input type="text" name="country_code" onkeyup="validatephone(this);" id="phone"/>


Hope This code might help you.

Comments or Responses

Login to post response