2

so I have a sign up form and I want to validate it with the jQuery Validation. I'm a beginnner and I'm not really sure how it works..
Lets say I have an input of a phone number:

<label class="labelinput" for="phonenum">Phone Number: </label>
<input type="text" id="cell" name="cell" class="forminput" /><br /> 

And I want to validate with jQuery that the first digit is zero and that it's length is 10 digits. How can I do that?

Ethan
  • 163
  • 2
  • 12

3 Answers3

1

You can do like this

$(function(){
var phonenum = $('#cell').val();

if(!isNaN(parseFloat(phonenum )) && isFinite(phonenum ) && phonenum.length==10 && phonenum.charAt(0)=='0' ){
   alert('Correct Input');

}
else{
  alert('Wrong Input');
}

});

NOTE:

!isNaN(parseFloat(phonenum )) && isFinite(phonenum )

The above condition checks that the entered charcters are numeric characters.

This was just a simple solution.

If you want to use Regex you might want to take a look at this SO Question

Community
  • 1
  • 1
Abhinav
  • 8,028
  • 12
  • 48
  • 89
0

Try this query like

if($('#cell').val().length!=10 && $('#cell').val().charAt(0)!='0')
{
  alert('Wrong number input');
}
Mukesh Kalgude
  • 4,814
  • 2
  • 17
  • 32
0

You could try a regular expression:

if (!/^[0]\d{9}$/.test($('#cell').val())) {
    // wrong phone number
}

Fiddle

putvande
  • 15,068
  • 3
  • 34
  • 50
  • Thanks but how can I use that in jQuery validation? How do I use that as one of the rules? – Ethan Jun 27 '15 at 11:04