正则表达式匹配9个字符,其中前2个是字母,然后是6个数字,然后是1个字母

时间:2020-12-19 21:45:26

I have an asp textbox control, now using regular expression I want to validate that control, which should contain 9 characters where first 2 are letters and then 6 numbers and then 1 letter. Any help would be appreciated.

我有一个asp文本框控件,现在使用正则表达式我要验证该控件,该控件应包含9个字符,其中前2个是字母,然后是6个数字,然后是1个字母。任何帮助,将不胜感激。

1 个解决方案

#1


3  

If I know my Regex, you could use this: ^[a-zA-Z]{2}[\d]{6}[a-zA-Z]{1}$.

如果我知道我的正则表达式,你可以使用它:^ [a-zA-Z] {2} [\ d] {6} [a-zA-Z] {1} $。

(The {1} at the end isn't needed, but I though it looks good...)

(不需要最后的{1},但我看起来不错......)

How you check that is up to you. You can do this in code-behind (since you're using asp.net) or JS.

你如何检查这取决于你。您可以在代码隐藏(因为您使用的是asp.net)或JS中执行此操作。

EDIT: Should also work with a ValidationExpression.

编辑:也应该使用ValidationExpression。

Here's a sample of this in use:

以下是使用中的示例:

<asp:TextBox ID="TB1" runat="server" />
<asp:RegularExpressionValidator ID="validator" runat="server" ControlToValidate="TB1" ErrorMessage="2 letters, 6 digits and a letter, hotshot!" ValidationExpression="^[a-zA-Z]{2}[\d]{6}[a-zA-Z]{1}$" />

正则表达式匹配9个字符,其中前2个是字母,然后是6个数字,然后是1个字母

#1


3  

If I know my Regex, you could use this: ^[a-zA-Z]{2}[\d]{6}[a-zA-Z]{1}$.

如果我知道我的正则表达式,你可以使用它:^ [a-zA-Z] {2} [\ d] {6} [a-zA-Z] {1} $。

(The {1} at the end isn't needed, but I though it looks good...)

(不需要最后的{1},但我看起来不错......)

How you check that is up to you. You can do this in code-behind (since you're using asp.net) or JS.

你如何检查这取决于你。您可以在代码隐藏(因为您使用的是asp.net)或JS中执行此操作。

EDIT: Should also work with a ValidationExpression.

编辑:也应该使用ValidationExpression。

Here's a sample of this in use:

以下是使用中的示例:

<asp:TextBox ID="TB1" runat="server" />
<asp:RegularExpressionValidator ID="validator" runat="server" ControlToValidate="TB1" ErrorMessage="2 letters, 6 digits and a letter, hotshot!" ValidationExpression="^[a-zA-Z]{2}[\d]{6}[a-zA-Z]{1}$" />

正则表达式匹配9个字符,其中前2个是字母,然后是6个数字,然后是1个字母