Mark
Mark

Reputation: 4883

Jquery / Regex Validation

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

Answers (3)

Nandakumar
Nandakumar

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"
}
}
});

Plugin

Upvotes: 0

Aditya Singh
Aditya Singh

Reputation: 9612

Try this out:- http://jsfiddle.net/adiioo7/Whb55/

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

falsetru
falsetru

Reputation: 369444

Use test method of regular expression object:

> /1/ == '1'
false
> /1/.test('1')
true

if (reg.test($(z).val())) {
    alert("Valid")
}

Upvotes: 2

Related Questions