xVal 和正则表达式匹配

发布于 2024-08-27 00:14:49 字数 1506 浏览 5 评论 0原文

我正在使用 xVal 在 asp.net MVC 1.0 中验证我的表单,

不确定为什么我的正则表达式无法正确验证。

  • 使用空值进行验证
  • 使用“1”、“12”、“123”或“1234”值进行验证
  • 它使用的值 “12345”
  • 使用“12345”的值进行验证
  • 它使用“12345 -”的值进行验证
  • 它使用“12345 -1”的值进行验证
  • 它使用“12345 -12”的值进行验证...等等

对于邮政编码 我期望两种模式之一:

1234512345 -1234

以下是我尝试过的两个正则表达式:

(\d{5})((( -)(\d{4}))?)

(\d{5})|(\d{5} -\d{4})

这是我的 xVal 的 MetaData 类

[MetadataType(typeof(TIDProfileMetadata))]
public class TIDProfileStep
{
   public class TIDProfileMetadata
   {
       [Required(ErrorMessage = " [Required] ")]
       [RegularExpression(@"(\d{5})|(\d{5} -\d{4})", ErrorMessage = " Invalid Zip ")]
       public string Zip { get; set; }
   }
}

这是我的 aspx 页面:

<% Html.BeginForm("Edit", "Profile", FormMethod.Post); %>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
   <td>
      <h6>Zip:</h6>
   </td>
   <td>
      <%= Html.TextBox("Profile.Zip")%>
   </td>
</tr>
<tr>
   <td>
      <input type="submit"/>
   </td> 
</tr>
</table>
<% Html.EndForm(); %>

<% Html.Telerik().ScriptRegistrar()
        .OnDocumentReady(() =>
   { %>
   <%= Html.ClientSideValidation<TIDProfileStep>("Profile").SuppressScriptTags() %>
<% }); %>

I am using xVal to validate my forms in asp.net MVC 1.0

Not sure why my regular expression isn't validating correctly.

  • It does NOT validate with an empty value
  • It does NOT validate with the value of "1", "12", "123", or "1234"
  • It validates with the value of
    "12345"
  • It validates with the value of "12345 "
  • It validates with the value of "12345 -"
  • It validates with the value of "12345 -1"
  • It validates with the value of "12345 -12" ... etc

For a zip code I expect one of the two patterns:

12345 or 12345 -1234

Here are the two regex I tried:

(\d{5})((( -)(\d{4}))?)

(\d{5})|(\d{5} -\d{4})

Here is my MetaData class for xVal

[MetadataType(typeof(TIDProfileMetadata))]
public class TIDProfileStep
{
   public class TIDProfileMetadata
   {
       [Required(ErrorMessage = " [Required] ")]
       [RegularExpression(@"(\d{5})|(\d{5} -\d{4})", ErrorMessage = " Invalid Zip ")]
       public string Zip { get; set; }
   }
}

Here is my aspx page:

<% Html.BeginForm("Edit", "Profile", FormMethod.Post); %>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
   <td>
      <h6>Zip:</h6>
   </td>
   <td>
      <%= Html.TextBox("Profile.Zip")%>
   </td>
</tr>
<tr>
   <td>
      <input type="submit"/>
   </td> 
</tr>
</table>
<% Html.EndForm(); %>

<% Html.Telerik().ScriptRegistrar()
        .OnDocumentReady(() =>
   { %>
   <%= Html.ClientSideValidation<TIDProfileStep>("Profile").SuppressScriptTags() %>
<% }); %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半步萧音过轻尘 2024-09-03 00:14:49

您缺少开始和结束锚点:

^\d{5}( -\d{4})?$

没有这些,您就允许部分匹配。正则表达式通过 \d{5} 匹配字符串 12345 -112345-1< /code>,并验证它。

You're missing the start and end anchors:

^\d{5}( -\d{4})?$

Without these you allow partial matching. The regex matches the string 12345 -1 by \d{5}: 12345-1, and validates it.

温柔戏命师 2024-09-03 00:14:49

我没有提到我在输入字段中使用了掩码插件。
掩码插件可以在此处找到。

因此,在文本框中,如果我只填写前 5 位数字,然后按 Tab 键切换到下一个字段,由于我使用了掩码插件,将验证为 false。掩码插件会在可能为空的情况下输入下划线字符...因此,例如:

_____ -____ 将是将其放置在焦点上的空字段中的掩码。如果我填写前 5 位数字,我将得到:

12345 -____

然后,如果我按 Tab 切换到下一个字段,-____ 将被删除,但输入字段需要在blur上重新验证。

所以我所做的就是在模糊时重新验证输入字段,现在它可以工作了。

$('#Zip').blur(function() {
        $(this).validate();
});

我使用这个正则表达式 ^\d{5}( -\d{4})?$

I failed to mention that I was using a mask plugin for my input field.
The mask plugin can be found here.

So on the text box if I were to fill in only the first 5 digits and then tab to the next field is would validate as false due to the mask plugin I have used. The mask plugin, puts in an underscore character for empty possiblities.... So for example:

_____ -____ would be the mask that it would put in the empty field on focus. If I fill in the first 5 digits I would have:

12345 -____

Then if I tab to the next field, the -____ is removed, but the input field needs to be re-validated onblur.

So what I did was re-validate the input field on blur and now it works.

$('#Zip').blur(function() {
        $(this).validate();
});

I use this regex ^\d{5}( -\d{4})?$

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文