0

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?]

1
  • Besides the correction that @falsetru pointed out, your regex itself is not the cleanest. You don't want to allow dashes? You can reduce it down to this without changing the meaning at all: /[2-9]\d{9}$/. Unless you are trying to use the groups created by all those parens. Commented Nov 20, 2013 at 15:27

3 Answers 3

2

Use test method of regular expression object:

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

if (reg.test($(z).val())) {
    alert("Valid")
}
Sign up to request clarification or add additional context in comments.

Comments

1

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>

2 Comments

Great simple answer, not very customizable. Not fully cross browser compatable
This post could be helpfull to more customize this answer stackoverflow.com/questions/10038941/jquery-regex-validation
0

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

1 Comment

I'm not able to for this project.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.