I need to validate my description text-area for the text in that will contain only 2 or less than 2 digits .It will include text also.I have tried with **regex**
but I have no result.I have tried like
if(value.match(/^\d{1,2}$/)){
return false;
}else{
return true;
}
Means
Valid :
this is 22 years old
In Valid :
this is 222 years old
Can anyone suggest me any solution.Thanks in advance.
Without any implementing code it is hard to say...
But you'll want to remove the ^
and $
from your regex, they state begining and end of the string respectively.
You'd be better on trying to find numbers of more than 2 digits and return true if found:
/\d{3,}/
This will be true for all strings that include numbers of 3 digits so you know it is not good.
EDIT:
if(!value.match(/\d{3,}/)){
return false;
}else{
return true;
}
//Or, as Julian Descottes *almost* pointed out, simply return the value of the function
return !!value.match(/\d{3,}/);
Re-reading OP, I am not sure if this is exactly the expected behaviour as it checks for NUMBERS of 3digits or more, but does not count the amount of digits in the text allowing
I am 24 and my brother is 29
This has 4 digits, should it be good or bad?
^\\d{1,2}$
won't match any text, but it will match both the start and end of the string. So, it will match "34" but not "1934" or "Babylon 5" .
What you probably want is ^\\D*\\d{1,2}\\D*$
. The flanking \\D*'s mean "match 0 to an infinite number of non-digit characters."
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.