Reputation: 4883
I have the following input.
<input type="tel" id="Phone maxlength="10"/>
I'm trying to use Jquery amd regex to validate the number's format.
The jquery / regex is as follows.
$(function () {
var reg = /(([2-9]{1})([0-9]{2})([0-9]{3})([0-9]{4}))$/
var Input = $("input#phone").val();
if ($(z).val() == (reg)) {
alert("Valid")
}
});
This is not working for me, does anyone see why? Or is there a better way to do this?]
Upvotes: 0
Views: 3458
Reputation: 1101
Dude, Use Validate Plugin for Validation Example:
$(".selector").validate({
rules: {
// simple rule, converted to {required:true}
// compound rule
/*Phone is the name of the input*/
phone: {
required: true,
minLength: 10,
maxLength: 10
}
},
messages: {
phone: {
required: "We need your Phone no to contact you",
minLength: "Your email address must be 10 Characters",
maxLength: "Your email address must be 10 Characters"
}
}
});
Upvotes: 0
Reputation: 9612
HTML:-
<form>
<input type="tel" id="Phone" maxlength="10" pattern="[2-9]{1}[0-9]{2}[0-9]{3}[0-9]{4}" required>
<input type="submit" value="Submit" />
</form>
Upvotes: 1