Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript variable less than 4 characters

I have "name" JavaScript variable. If variable "name" contains less than 4 characters I want to execute line: msg('name','Your name must contain minimum 4 characters.')'; I have tried something like this but it interpretated mathematical. Any idea? Thank you.

if(name < 4 ) {
  msg('name','Your name must contain minimum 4 characters.');
  return false;
}
like image 691
CBuzatu Avatar asked Nov 28 '22 02:11

CBuzatu


2 Answers

if (name.length < 4) {
   ...
}
like image 71
Mark Reed Avatar answered Dec 05 '22 08:12

Mark Reed


You probably want to check the length of the string, not the numeric value of the string itself:

if(name.length < 4) {
    // ...
like image 41
icktoofay Avatar answered Dec 05 '22 08:12

icktoofay