如何验证文本框至少有一个字母和一个数字允许一些特殊字符? [英] How to validate textbox have at least one letter and one number allow some special characters?
问题描述
我想验证文本框。
test_100允许
test100允许
test-100_01允许
以上格式是允许的。如果文本框只有符号抛出错误。
我尝试过:
正则表达式正则表达式= 新正则表达式( @ ^ [a-zA-Z0-9,.- \\ -_] * $);
if (!regex.Match(textbox1.Text).Success)
{
Response.Write( 正确输入数字)
}
i有使用上面的代码。这个方法满足条件但是
- (或)__(或),,
在文本框中输入上面的符号allowed.but我想不允许throw错误消息。
);
if (!regex.Match(textbox1.Text).Success)
{
Response.Write( 正确输入数字)
}
i使用了上面的代码。这种方法满足条件但是
- (或)__(或),,
在允许的文本框中输入上面的符号。但我不想允许抛出错误消息。
我是我的人非常厌恶使用RegEx。怎么样:
if ( string .IsNullOrEmpty((textbox1.Text.Replace( - , )。替换( _, )。 ( ,, ))
{
// 显示错误
}
你也可以这样做:
string symbols = -_,;
if (textbox1.Text.Where( x =>!symbols.Contains(x))。Count()== 0 )
{
// 显示错误
}
我不知道不喜欢使用正则表达式并且必须查找它们,但是为了允许1个字符,1个数字和1个常见的特殊字符,我会执行以下操作。
如果仔细观察,搜索模式位于圆括号之间。
< !DOCTYPE html >
< html lang = zh >
< head >
< title < span class =code-keyword>> 验证密码< / title < span class =code-keyword>>
< script type = < span class =code-keyword> text / javascript >
function CheckPassword(txtPassword)
{
var regEx = /^(?=.*[az])(?=.*[0 -9])(?=。* [!@#\
I want to validate text box.
test_100 allowed
test100 allowed
test-100_01 allowed
above format is allowed.if text box have only symbols throw the error.
What I have tried:
Regex regex = new Regex(@"^[a-zA-Z0-9 ,.-\\-_]*$");
if (!regex.Match(textbox1.Text).Success)
{
Response.Write("Enter number correctly")
}
i have used above code.this method satisfy the condition but
-- (or) __ (or) ,,
enter above symbols in textbox allowed.but i want not allow throw error message.
"); if (!regex.Match(textbox1.Text).Success) { Response.Write("Enter number correctly") }
i have used above code.this method satisfy the condition but
-- (or) __ (or) ,,
enter above symbols in textbox allowed.but i want not allow throw error message.
I personally abhor the use of RegEx. How about this:
if (string.IsNullOrEmpty((textbox1.Text.Replace("-", "").Replace("_", "").Replace(",","")) { // show error }
You could also do it this way:
string symbols = "-_,"; if (textbox1.Text.Where(x=>!symbols.Contains(x)).Count() == 0) { // show error }
I don't like using Regular Expressions and had to look them up, but to allow 1 character, 1 number, and 1 common special character, I'd do the following.
And if you take a closer look, your search pattern is in between the rounded brackets.
<!DOCTYPE html> <html lang="en"> <head> <title>Validate Passwords</title> <script type="text/javascript"> function CheckPassword(txtPassword) { var regEx = /^(?=.*[a-z])(?=.*[0-9])(?=.*[!@#\
这篇关于如何验证文本框至少有一个字母和一个数字允许一些特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!