如何验证数字字母和所有特殊字符 [英] how to validate for numbers alphabets and all special characters
本文介绍了如何验证数字字母和所有特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在c#.net中,对于一个文本框,它应该接受所有数字,字母,特殊字符,但是当我单独给出数字时,它应该抛出错误,因为输入字母也...如何编码...任何人都可以帮助me.please ..
in c#.net,for a textbox,it should accept all numbers,alphabets,special characters but when i give numbers alone it should throw error as"enter alphabets also"...how to code...can anyone help me.please..
推荐答案
选项1:使用 C#Regex类 [ ^ ]。要检查的条件:至少一个非数字字符。因此,接受模式可以是@\ D
。例如
Option 1: Use C# Regex class[^]. The codition to check: at least one non-digit character. So, the acceptance pattern could be@"\D"
. E.g.
bool IsOk(s) { return Regex.IsMatch(s, @"\D"); }
选项2:循环遍历字符串的所有字符并使用 char.IsDigit [ ^ ]方法如果至少有一个字符不是数字,例如
Option 2: loop over all characters of the string an check with char.IsDigit[^] method if at least one character is not a digit, e.g.
bool IsOk(string s) { return s.Length > 0 && s.Any(c=>!char.IsDigit(c));
干杯
Andi
Cheers
Andi
http://www.9lessons.info /2009/03/perfect-javascript-form-validation.html [ ^ ]
这篇关于如何验证数字字母和所有特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文