[英]Validation to check if the user has entered the correct format
I have a dynamic grid that has a numeric field in which I have applied masking.我有一个动态网格,其中有一个数字字段,我在其中应用了掩码。 Also it has a plus button means the user can add many rows as he wants.
它还有一个加号按钮,意味着用户可以根据需要添加许多行。 The masking is applied like this:
屏蔽是这样应用的:
<input type='textbox' placeholder='00000-00-000' data-mask='00000-00-000'
This masking is applied when the user enters 10 digit number but it lets the user enter a 2 or 3 digit number as well.当用户输入 10 位数字时应用此屏蔽,但它也允许用户输入 2 或 3 位数字。 For this I am trying to apply validation while saving so that it checks whether the value entered matches the required format.
为此,我尝试在保存时应用验证,以便检查输入的值是否与所需格式匹配。
What I have done so far is:到目前为止我所做的是:
value = $(this).find('td:eq(1)').find('input').val(); //saves the entered value in a variable value
myRegExp = new RegExp(/\d{5}-\d{2}-\d{3}/);
if (!myRegExp.test(value)) {
valid = false;
}
else
valid = true;
The value that user enters is saved in varaible called value and then I have defined my Regex Expression to match my value with that expression but somehow this regex expression is not working.用户输入的值保存在名为 value 的变量中,然后我定义了我的正则表达式以将我的值与该表达式匹配,但不知何故,这个正则表达式不起作用。 Is this the correct way to define regex expression?
这是定义正则表达式的正确方法吗? What am I missing here?
我在这里想念什么? My required format is 00000-00-000.
我要求的格式是 00000-00-000。 Any help would be appreciated.
任何帮助,将不胜感激。
Your logic is correct but you have not defined the end point that's why it allows to insert more values.您的逻辑是正确的,但您尚未定义终点,这就是它允许插入更多值的原因。
In your Regex it only checks if the 10 digits are in the specific order在您的正则表达式中,它仅检查 10 位数字是否按特定顺序
try out this试试这个
myRegExp = new RegExp(/^\d{5}-\d{2}-\d{3}$/);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.