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
JavaScript
x
13
13
1
<form name="enquiry_form" method="post" id="enquiry_form">
2
3
Full Name *
4
<input class="input-style" name="name" id="name" type="text" required>
5
Email *
6
<input class="input-style" name="email" id="email" type="email" required>
7
Phone *
8
<input name="mobile" id="mobile" type="number" required>
9
10
<input type="submit" value="SUBMIT" id="enq_submit"">
11
12
</form>
13
Advertisement
Answer
for email validation, <input type="email">
is enough..
for mobile no use pattern attribute for input as follows:
JavaScript
1
2
1
<input type="number" pattern="d{3}[-]d{3}[-]d{4}" required>
2
you can check for more patterns on http://html5pattern.com.
for focusing on field, you can use onkeyup() event as:
JavaScript
1
18
18
1
function check()
2
{
3
4
var mobile = document.getElementById('mobile');
5
6
7
var message = document.getElementById('message');
8
9
var goodColor = "#0C6";
10
var badColor = "#FF9B37";
11
12
if(mobile.value.length!=10){
13
14
mobile.style.backgroundColor = badColor;
15
message.style.color = badColor;
16
message.innerHTML = "required 10 digits, match requested format!"
17
}}
18
and your HTML code should be:
JavaScript
1
2
1
<input name="mobile" id="mobile" type="number" required onkeyup="check(); return false;" ><span id="message"></span>
2