Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate Text Field Length In Between 2 Numbers

I am trying to validate whether a HTML field is between 11 and 13 characters long.

I have tried using:

if(!phoneCheck.test(phone) || (phone.length !>= 11 && phone.length !<=13))

But this does not seem to work. It just stops all Javascript as if there is an error in it.

How would i go about doing this?

EDIT:

Sorry i shouldve added a further part of the code:

if(!phoneCheck.test(phone) || (phone.length !>= 11 && phone.length !<=13))
{
error = "Please give a valid phone number.";
}

The exclamation mark was added so that if it WAS NOT between those lengths then it would trigger the error

like image 471
Dr.Pepper Avatar asked Sep 11 '25 21:09

Dr.Pepper


1 Answers

The symbols !>= and !<= don't exist. The condition code to check if field is between 11 and 13 characters long is:

(phone.length >= 11 && phone.length <= 13)

OBS:
<= means "less or equal" and >= means "bigger or equal".
! is the logical denial.
For example:
!true is false
and
!(a>b) is true when a<=b

EDIT: With your edit, what you want is to deny the entire sentence, so the solution is:

!(phone.length >= 11 && phone.length <= 13)

So your if statement should be:

if(!phoneCheck.test(phone) || !(phone.length >= 11 && phone.length <=13))
like image 84
DanielX2010 Avatar answered Sep 13 '25 11:09

DanielX2010