How to do validation for 10 digit mobile number Using jQuery
This will work for you...
$(document).ready(function(){ $("#mobile-num").on("blur", function(){ var mobNum = $(this).val(); var filter = /^\d*(?:\.\d{1,2})?$/;
if (filter.test(mobNum)) { if(mobNum.length==10){ alert("valid"); $("#mobile-valid").removeClass("hidden"); $("#folio-invalid").addClass("hidden"); } else { alert('Please put 10 digit mobile number'); $("#folio-invalid").removeClass("hidden"); $("#mobile-valid").addClass("hidden"); return false; } } else { alert('Not a valid number'); $("#folio-invalid").removeClass("hidden"); $("#mobile-valid").addClass("hidden"); return false; } }); });
.hidden{ display: none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="mobile"> <div class="input"> <div class="label"> <div class="label-1"> <span id="mobile-valid" class="hidden mob"> <i class="fa fa-check pwd-valid"></i>Valid Mobile No </span> <span id="folio-invalid" class="hidden mob-helpers"> <i class="fa fa-times mobile-invalid"></i>Invalid mobile No </span> </div> </div> <div class="row"> <div class="col-xs-3"> <div class="label-2"> Mobile No </div> </div> <div class="col-xs-9"> <input id="mobile-num" type="text" class="form-control form-change-element" /> </div> </div> </div> </div>
Validation for 10 digit mobile number and focus input field on invalid
for email validation, <input type="email">
is enough..
for mobile no use pattern attribute for input as follows:
<input type="number" pattern="\d{3}[\-]\d{3}[\-]\d{4}" required>
you can check for more patterns on http://html5pattern.com.
for focusing on field, you can use onkeyup() event as:
function check()
{
var mobile = document.getElementById('mobile');
var message = document.getElementById('message');
var goodColor = "#0C6";
var badColor = "#FF9B37";
if(mobile.value.length!=10){
mobile.style.backgroundColor = badColor;
message.style.color = badColor;
message.innerHTML = "required 10 digits, match requested format!"
}}
and your HTML code should be:
<input name="mobile" id="mobile" type="number" required onkeyup="check(); return false;" ><span id="message"></span>
Mobile number validation not working properly using jquery validation
Using addMethod
$(document).ready(function() { $.validator.addMethod( "mobileValidation", function(value, element) { return !/^\d{8}$|^\d{10}$/.test(value) ? false : true; }, "Mobile number invalid" );
$("#form").validate({ rules: { mobile: { required: true, mobileValidation: $("#mobile").val(), }, }, }); });
<script src="https://code.jquery.com/jquery-3.4.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.0/dist/jquery.validate.min.js"></script> <form id="form"> <input type="number" id="mobile" name="mobile" /> <input type="submit" name="submit" value="submit" /> </form>
Regex for 10-15 digit phone number validation
The following pattern would seem to work here:
^\+(?=.{15})\d{10,15}_{0,5}$
This uses a positive lookahead to assert that exactly 15 characters, excluding the leading +
, occur in the input. With that assertion in place, we can match for 10-15 digits, followed by 0-5 underscores.
Demo
Phone Number Validation. Allow only 0 as first digit
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="phone-number"/>
$(document).on('input','#phone-number',function(){
var phone=$('#phone-number').val();
if(phone.indexOf('0')!==0){
alert('First number must be 0');
$('#phone-number').val('');
}
});
how to validate the mobile number in jquery
Use this Jquery onchange function
$("#ph").change(function() {
var a = $("#ph").val();
var filter = /^[7-9][0-9]{9}$/;
if (filter.test(a)) {
alert("valid");
}
else {
alert("not valid");
}
});
Check this fiddle http://jsfiddle.net/GxuA7/35 click on this link and see the vaildation
Related Topics
Check If a Key Exists Inside a Json Object
Postman Test - Validating String Values in an Object in an Array
How to Compare Two Objects and Get Key-Value Pairs of Their Differences
Regular Expression Which Matches a Pattern, or Is an Empty String
Innerhtml Inserts Only [Object Htmldivelement]
I Want to Change the Page Background Color, But It Doesn't Work as I Expect in Chrome
How to Replace a String With Square Brackets Using JavaScript Replace Function
Call JavaScript Function Onchange Event of Dropdown List
React Js How to Trigger an Event to Another Div
Finding Sequential Values in Js Array
How to Toggle Class to a Div Element in Reactjs
Hosting Your Own JavaScript Scripts Files (Other Than Jquery) on Fast Free Cdns Like Google
How to Get the Latest and Oldest Record in Mongoose.Js (Or Just the Timespan Between Them)
Filereader Onload With Result and Parameter
How to Create Prompt With Two Input Fields
How to Detect the Mime Type of Data Url