在ASP.NET中使用正则表达式进行文本框验证 [英] Text box validation using regular expression in asp.net
问题描述
我有一个文本框,必须使用asp.net中的正则表达式
I have a textbox and have to use the regular expressions in asp.net
我的文字不应在首尾放置空格.
My text should not allow the spaces in first and last place.
有效: [印度班加罗尔]
无效: [印度班加罗尔]
即:用户可以在单词之间输入空格,但不能在第一个位置和最后一个位置输入
i.e : user can enter the spaces in between the words but not in first position and last position.
如果您有JavaScript的解决方案,也可以.
If you have solution in JavaScript that is also fine.
推荐答案
请尝试以下操作:
^[^\s].*[^\s]$
它只是匹配以下输入:不要以任何空格 ^ [^ \ s]
开头,后跟任何字符,甚至是空格.*
,也不要以任何空格 [^ \ s] $
.
It simply match input which:
not to start with any white space ^[^\s]
followed by any character even white spaces .*
and not end with any white space [^\s]$
.
在服务器端对输入字符串调用Trim()方法的任何方式都非常容易.
Any way calling Trim() method on input string in server-side is much easy .
这篇关于在ASP.NET中使用正则表达式进行文本框验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!