I need the code for validating email and mobile number in jQuery and also focus()
on that particular field where validations are not satisfied.
This is my query
<form name="enquiry_form" method="post" id="enquiry_form">
Full Name *
<input class="input-style" name="name" id="name" type="text" required>
Email *
<input class="input-style" name="email" id="email" type="email" required>
Phone *
<input name="mobile" id="mobile" type="number" required>
<input type="submit" value="SUBMIT" id="enq_submit"">
</form>
Mobile Number validation criteria:The first digit should contain numbers between 6 to 9. The rest 9 digit can contain any number between 0 to 9. The mobile number can have 11 digits also by including 0 at the starting. The mobile number can be of 12 digits also by including 91 at the starting.
Let's say that the genuine phone number is 10 digits long, ranging from 0 to 9. To perform the validation, you have to utilize this regular expression: /^[0-9]{10}+$/.
Above code is correct but mobile validation is not perfect.
i modified as
$('#enquiry_form').validate({
rules:{
name:"required",
email:{
required:true,
email:true
},
mobile:{
required:true,
minlength:9,
maxlength:10,
number: true
},
messages:{
name:"Please enter your username..!",
email:"Please enter your email..!",
mobile:"Enter your mobile no"
},
submitHandler: function(form) {alert("working");
//write your success code here
}
});
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With