Surya sasidhar
Surya sasidhar

Reputation: 30303

validation on textbox (no space)

i have a webpage in that i have a text box, my requirement is i don't want to give space in the textbox if user give space in textbox it give indication no space in the textbox

Upvotes: 4

Views: 18726

Answers (4)

Sagar Jadhav
Sagar Jadhav

Reputation: 129

You can use HTML page or Asp.Net page only pattern="[^\s]+" in side TextBox

<input id="Text1" pattern="[^\s]+" type="text" />

Upvotes: 0

akhilesh mishra
akhilesh mishra

Reputation: 1

It might help you all to remove your spaces in your document please try It.

<asp:TextBox runat="server" ID="txttitlename" />
<asp:RegularExpressionValidator runat="server" ErrorMessage="Spaces are not acceptable" ontrolToValidate="txttitlename" ValidationExpression="[^\s]+" />

Upvotes: 0

Rex M
Rex M

Reputation: 144112

You can use the RegularExpressionValidator control:

<asp:TextBox runat="server" ID="txt1" />
<asp:RegularExpressionValidator 
  runat="server" ErrorMessage="Spaces are not permitted" 
  ControlToValidate="txt1"
  ValidationExpression="[^\s]+" />

The pattern [^\s]+ means "one or more characters that is not a space". So if any of the characters is a space, it will fail.

Upvotes: 2

Ahmad Mageed
Ahmad Mageed

Reputation: 96477

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"
    ErrorMessage="Spaces are not allowed!" ValidationExpression="[^\s]+" />
<asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtBox" 
    ErrorMessage="Value can't be empty" />

This would prevent "hello world" and "data base" since they contain spaces, and would only allow "helloworld" and "database" as valid values. You must use a RequiredFieldValidator in conjunction with it to prevent blank entries since the RegularExpressionValidator does not prevent that on its own.

Specify the name of the textbox in the ControlToValidate property.

Upvotes: 9

Related Questions