如何避免文本框中的空间 [英] How to avoid space in textbox

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

问题描述

我有一个TextBox..



I have a TextBox..



<asp:TextBox ID="pwdPassword" runat="server" MaxLength="16"  TextMode="Password" Width="144px"  onKeyUp="CountLeft(8);"><br />




我需要避免在我的文本框中出现空格




i need to avoid space in my textbox

推荐答案

如果要捕获空格不是有效字符的值,则可以使用RegularExpressionValidator

If you intended to capture a value where spaces aren''t a valid character, you could use a RegularExpressionValidator

<asp:regularexpressionvalidator id="rev" runat="server" controltovalidate="txtBox" xmlns:asp="#unknown">
    ErrorMessage="Spaces are not allowed!" ValidationExpression="[^\s]+" />
<asp:requiredfieldvalidator id="rfv" runat="server" controltovalidate="txtBox">
    ErrorMessage="Value can''t be empty" /></asp:requiredfieldvalidator></asp:regularexpressionvalidator>



在aspx.cs页面中



In aspx.cs page

void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
     if ((sender as TextBox).SelectionStart == 0)
          e.Handled = (e.KeyChar == (char)Keys.Space);
     else
          e.Handled = false;
}



他们有很多避免空格的方法

谢谢&问候,
SP



Their are many ways to avoid spaces

Thanks & Regards,
SP



您需要使用JavaScript.看看这篇文章,其中JavaScript已被用来禁止使用各种字符:
http://www.tek-tips.com/viewthread.cfm?qid=1581320 [ ^ ]
希望对您有帮助,
干杯
Hi,
You need to use JavaScript. Take a look at this post which JavaScript has been used to disallow various characters:
http://www.tek-tips.com/viewthread.cfm?qid=1581320[^]
I hope it helps,
Cheers


Sridhar Patnayak 的解决方案1很好.但是验证表达式[^\s]+不允许在匹配项内包含空格,但在匹配项周围允许包含空格.因此,例如.在文本框中输入Two Words,它将匹配Two Words.为了确保在TextBox 中的任何位置都不允许有空间,请使用ValidationExpression "^ [^ \ s] +
The Solution 1 by Sridhar Patnayak is very good. But the validation expression [^\s]+ does not allow spaces within the match but allows spaces surrounding the match. So, for eg. Two Words are entered in the TextBox, it will match Two and Words. To ensure that no space is allowed any where in the TextBox , the ValidationExpression "^[^\s]+


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

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