简体   繁体   English

如何限制2位数的描述

[英]How to restrict description for 2digits only

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 我需要验证我的描述文本区域的文本,其中只包含2个或少于2个数字。它也将包括文本。我试过**regex**但我没有结果。我试过像

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: 您最好尝试查找超过2位数的数字,如果找到则返回true:

/\d{3,}/

This will be true for all strings that include numbers of 3 digits so you know it is not good. 对于包含3位数字的所有字符串都是如此,因此您知道它不好。

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 重新读取OP,我不确定这是否完全是预期的行为,因为它检查3dBits或更多的NUMBERS,但不计算允许的文本中的位数

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. ^\\d{1,2}$将不匹配任何文本,但它将匹配字符串的开头和结尾。 So, it will match "34" but not "1934" or "Babylon 5" . 因此,它将匹配“34”而不是“1934”“巴比伦5”

What you probably want is ^\\D*\\d{1,2}\\D*$ . 你可能想要的是^\\D*\\d{1,2}\\D*$ The flanking \\D*'s mean "match 0 to an infinite number of non-digit characters." 侧翼\\ D *的意思是“将0与无限数量的非数字字符匹配”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM