验证文本框 [英] validation for textbox

查看:74
本文介绍了验证文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,我希望验证如下所示



如果用户只输入字母,它应该允许

如果用户输入字母数字应该允许

但如果用户只输入数字则不允许







i知道其中一些,但我不工作

^ [0-9] {1,17}:只允许数字

^ [a -zA-z] {1,50}:仅允许文本

^ [a-zA-Z0-9-\s&] {1,255}:仅允许使用字母数字文本





请帮我解决这个问题

I have a textbox in which i want validation like as per following

if a user enters only alphabets it should allow
if a user enters alphanumeric it should allow
but if a user enters only numbers it should not allow



i knew some of them but i doesnt work
^[0-9]{1,17} : Allow only number
^[a-zA-z]{1,50} : Allow Only Text
^[a-zA-Z0-9-\s&]{1,255} : Allow only Alphanumeric text


please help me through this

推荐答案

谁告诉你应该在任何地方使用Regex或在这种情况下它是最好的?仅在最合适的地方使用工具。



在这种情况下,一些非正则表达式方法应该简单易行且易于维护。注意:所有规则都不是基于任何特定模式,它们只与多样性相关。因此,计算分为3类的字符数:数字,字母和其余部分。为此,在 foreach 循环中遍历字符串并递增两个计数器中的一个(对于数字和字母,第三个计数将通过从字符串<$中减去来获得) c $ c>长度),使用两个函数:

http://msdn.microsoft.com/en-us/library/7f0ddtxh.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/yyxz6h5w.aspx [ ^ ]。



这就是全部。简单,不是吗?



-SA
Who told you that Regex should be used everywhere or it's the best in this case? Use the tools only where they are most adequate.

In this very case, some non-Regex approach should be way easy and more maintainable. Pay attention: all your rules are not based on any certain patterns, they are only related to multiplicity. Therefore, calculate number of characters classified into 3 classes: digits, letters and the rest of them. For that purpose, traverse the string in a foreach loop and increment one of the two counters (for digits and letters, the third count will be obtained by subtraction from the string Length), using two functions:
http://msdn.microsoft.com/en-us/library/7f0ddtxh.aspx[^],
http://msdn.microsoft.com/en-us/library/yyxz6h5w.aspx[^].

That's all. Simple, isn't it?

—SA


嗯,这个怎么样?< br $> b $ b

^ [a-zA-Z] * [0-9] * [a-zA-Z] + [0-9] *



我在Editplus上测试过它。经过测试的样本是这样的。

Well, what about this?

^[a-zA-Z]*[0-9]*[a-zA-Z]+[0-9]*

I have tested it on Editplus. Tested samples are like this.
asdfasdffd
1231232131
1232131adfasdfas
asdfs1231asdfas
asfsfs13123



搜索结果如下。


Searched results are like this.

asdfasdffd
1232131adfasdfas
asdfs1231asdfas
asfsfs13123



我希望它对你有帮助。


I hope it would be helpful to you.


这个必需的表达式是

^ [a-zA-Z \ _ _] [a-zA-Z0-9 \ _ _] +
the required expression for this is
^[a-zA-Z\s_][a-zA-Z0-9\s_]+


这篇关于验证文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆