简体   繁体   English

检测到文本框有一个数字和一个大写字母

[英]Detect that textbox has one number and one upper letter

Hi I need to use regular expressions in C# to validate a TextBox. 嗨我需要在C#中使用正则表达式来验证TextBox。

Regular expressions should validate that the textbox has one number and one uppercase letter 正则表达式应验证文本框是否包含一个数字和一个大写字母

My code is: 我的代码是:

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
    ControlToValidate="TextBox1" ErrorMessage="RegularExpressionValidator" 
    ValidationExpression="[A-Z a-z 0-9]*[0-1]+[A-z]+"></asp:RegularExpressionValidator>

but it's not allowing 但它不允许

nameA123

Where is my error? 我的错误在哪里?

Use this regex: 使用这个正则表达式:

^(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]+$

In the regex demo , you can add strings to see if they match. 正则表达式演示中 ,您可以添加字符串以查看它们是否匹配。

Explanation 说明

  • The ^ anchor asserts that we are at the beginning of the string ^ anchor断言我们位于字符串的开头
  • The lookahead (?=.*[AZ]) asserts that at it is possible to match some chars then an uppercase letter 前瞻(?=.*[AZ])断言可以匹配一些字符然后一个大写字母
  • The lookahead (?=.*[0-9]) asserts that at it is possible to match some chars then a digit 前瞻(?=.*[0-9])断言,有可能匹配一些字符,然后匹配一个数字
  • [a-zA-Z0-9]+ matches letters and digits [a-zA-Z0-9]+匹配字母和数字
  • The $ anchor asserts that we are at the end of the string $ anchor断言我们在字符串的末尾

Reference 参考

The below regex would validates for atleast one uppercase alpha and one number and it won't care about any character including special characters. 下面的正则表达式将验证至少一个大写字母和一个数字,它不会关心包括特殊字符在内的任何字符。

^(?=.*[A-Z])(?=.*[0-9]).*$

DEMO DEMO

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

相关问题 文本框:密码字符(将单词的字母一一转换为星号) - textbox: password character (changing the letter of the word into asterisk one by one) 将 TextBox 中的 ID 数量增加一个 - Increasing the number of ids in the TextBox by one 使文本框的第一个字母大写 - Making first letter of a textbox upper case .NET RegEx可以验证6到20个字符之间的密码,并至少包含一个大写字母,一个小写字母和一位数字 - .NET RegEx to validate password between 6 and 20 characters with at least one upper case letter, one lower case letter and one digit C#正则表达式,用于至少一个字母和至少一个数字 - C# Regex for at least one letter and at least one number 如何验证密码至少包含一个大写或小写字母? - How can I validate a password to contain at least one upper-case or lower-case letter? WPF TextBox 只接受一个数字 - WPF TextBox accepts only one number 在文本框中检测第一个字母,然后出现在gridview中 - Detect first letter in textbox then appears in gridview 检查字符串是否至少包含以下各项之一:小写字母、大写字母、数字和特殊字符 - Check if string contains at least one of each: lowercase letter, uppercase letter, number, and special character c# 中的控制文本框,它只允许 8 个数字和最后一个字母 - Control TextBox in c# that only admits 8 numbers and only one last letter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM