Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 15-Apr-12 23:36pm
v3
Comments
Dhanamanikandan 16-Apr-12 5:43am    
your question is to trim the input textbox value?

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>


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;
}


Their are many ways to avoid spaces

Thanks & Regards,
SP
 
Share this answer
 
Comments
VJ Reddy 16-Apr-12 5:55am    
Good answer. 5!
Sridhar Patnayak 16-Apr-12 8:20am    
Thanks Reddy
P.Salini 16-Apr-12 7:36am    
5'ed
Sridhar Patnayak 16-Apr-12 8:20am    
Thanks Shalini
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
 
Share this answer
 
Comments
Member 13686664 28-Mar-18 5:34am    
hi. is there any way to restrict users to type just 1 white space between words in text box. i use trim for space before and after words but in the middle of words is needed to be allowed just 1 space.
by the way I'm working with c# and windows form project.
many thanks
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]+$" can be used.
 
Share this answer
 
Comments
Sridhar Patnayak 16-Apr-12 8:19am    
Agree with you. 5+
VJ Reddy 16-Apr-12 8:37am    
Thank you, Sridhar.
<html>
<head>
<script type="text/javascript">

function blockSpace(e)
{
	var key;
	var isCtrl = false;
	var keychar;			
	if(window.event) {
		key = e.keyCode;
		isCtrl = window.event.ctrlKey
	}
	else if(e.which) {
		key = e.which;
		isCtrl = e.ctrlKey;
	}	
	keychar = String.fromCharCode(key);	
	if(keychar==" ")
	{
		return false;
	}	
}

</script>
</head>
<body>

<input type="text" onkeypress="return blockSpace(event);" />

</body>
</html>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900