How to validate a 10-digit mobile number Using jQuery Ajax? Only numbers are allowed cannot use characters or alphanumeric characters. So how can I use jquery Ajax to do validation?
There are different methods shown, choose any one which related to your code:
1. Jquery validation for the mobile number using regular expression
Validation includes:
- 10-digit number only (No alphabets and No special characters)
- Numbers start from 6,7,8,9
<html>
<body>
<label>Mobile Number:</label>
<input type="text" class="form-control mobile-valid">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('.mobile-valid').on('keypress', function(e) {
var $this = $(this);
var regex = new RegExp("^[0-9\b]+$");
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
// for 10 digit number only
if ($this.val().length > 9) {
e.preventDefault();
return false;
}
if (e.charCode < 54 && e.charCode > 47) {
if ($this.val().length == 0) {
e.preventDefault();
return false;
} else {
return true;
}
}
if (regex.test(str)) {
return true;
}
e.preventDefault();
return false;
});
});
</script>
</body>
</html>
10 Digit Mobile Number Validation Using JavaScript With JQuery
As per security validating the mobile numbers is the best way to get input from users as per defined conditions. In this article we have all about how to validate a mobile number (in a different format) using HTML/JavaScript/jQuery :
Try this Script:-
<script>
function mobilenumber(userstrtxt)
{var phoneno=/^\d{10}$/;if(userstrtxt.value.match(phoneno))
{return true;}
else
{alert("Not a valid Phone Number");return false;}}
</script>
JQuery:
<script type="text/javascript">
$("input[name=moblie_no]").attr("maxlength", "10");
$('.moblie-no').keypress(function(e) {
var arr = [];
var kk = e.which;
for (i = 48; i < 58; i++)
arr.push();
if (!(arr.indexOf(kk)>=0))
e.preventDefault();
});
</script>
Validate 10 Digits Mobile Number In ASP.NET
Note: Remember TextBox ID=” example”, update with your code. When you apply the RegularExpressionValidator TextBox ID=” example” paste it into the ControlToValidate=”example”
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br\>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="RegularExpressionValidator"
ValidationExpression="[0-9]{10}"></asp:RegularExpressionValidator>
Note: If you found any error in the code, please comment on the given below. We will try to resolve and update it ASAP.
Leave a comment