Adrian Florescu
Adrian Florescu

Reputation: 4492

jQuery - If input has more or less that 10 characters inside

I want to make a custom validation for an phone number input.

var telVal = $("#telefon").val();
if(telVal == '') {
$("#telefon").addClass('eroare png');
hasError = true;
}

This gives me an error if user does not type anything in this input, but I want to check if user types 10 characters. So, the script must be like

if(telVal has 9 characters or more than 10) { do this }

Thank you!

Upvotes: 2

Views: 6288

Answers (2)

Evan M
Evan M

Reputation: 2611

Plenty of people have done this before - you can probably find some more advanced parsing samples to include in your solution (such as checking for dashes, parentheses, etc.) on sites like this:

http://www.smartwebby.com/DHTML/phone_no_validation.asp

Upvotes: 2

Brandon
Brandon

Reputation: 69963

So basically, if it's not 10?

if(telVal.length != 10)

Upvotes: 7

Related Questions