简体   繁体   English

验证以检查用户是否输入了正确的格式

[英]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.

相关问题 使用javascript检查用户是否输入了正确的值 - Using javascript to check if user has entered a correct value 如何检查用户是否输入了打字值 - How to check if user has entered typeahed value 如何检查输入的 TOTP 密钥格式是否正确? - How to check if entered TOTP secret key format is correct? 如何使用react和javascript检查用户在输入字段中输入的输入是否具有格式为“10h”或“10m”的字符串? - How to check if the user entered input in input field has the string in format '10h' or '10m' using react and javascript? 检查 HTML 输入元素是否为空或没有用户输入的值 - Check if an HTML input element is empty or has no value entered by user 如何检查用户是否尚未将数据输入表单(用于发送)? - How to check if the user has not entered the data to a form (befor sending)? 如何检查用户是否在Javascript中输入了日期值? - How to check if the user has entered the value of date or not in Javascript? 如何检查文本字段值的格式是否正确? - How can I check if a text field value has the correct format? 正则表达式验证用户是否提供了正确的 URI 格式 - Regular Expression to validate if user has provided the correct URI format or not AngularJS + Bootstrap-验证文本框-仅在用户输入一些数据后如何显示验证反馈? - AngularJS + Bootstrap - Validating Text Box - how to show validation feedback only after the user has entered some data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM