1

I am using RegularExpressionValidator for FreeTextBox control in my aspx page.

<FTB:FreeTextBox id="FTB" runat="server" />
<asp:RegularExpressionValidator ID="rev" runat="server" ControlToValidate="FTB" ErrorMessage="Content cannot be only space character" ValidationExpression="[^\s]+"/>

I want to not allow the text to only have space characters. The client has to input some a,b,c… character.

But the RegularExpressionValidator denies any space character in the text (such as between 2 words).

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
vyclarks
  • 854
  • 2
  • 15
  • 39
  • Specifically for a regex validator the expression `[^\s]+` means that the *whole text* should be non-space: there is an implicit extra check that the matched part is the whole text. – Hans Kesting Nov 15 '13 at 08:30

3 Answers3

1

This regular expression .*[^ ].* matches a string only if it contains something more than spaces. I tested it here.

Hope I helped!

Pantelis Natsiavas
  • 5,293
  • 5
  • 21
  • 36
  • you mean I should change: ValidationExpression=".*[^ ].*" – vyclarks Nov 15 '13 at 07:29
  • When you say tested, you mean using [this](http://www.regular-expressions.info/javascriptexample.html) or the ASP.NET engine. Would you please tell me the not working scenario? (just to see if I got it right) – Pantelis Natsiavas Nov 15 '13 at 07:49
  • I test it as the way you did here http://www.regular-expressions.info/javascriptexample.html. Then I change ValidationExpression=".*[^ ].*" but the program still alows the string with many and only spaces characters – vyclarks Nov 15 '13 at 07:51
  • Strange! I supposed that the ValidationExpression in ASP.NET is used to validate the regular expression in javascript space, like the test site I told you. Perhaps, something is going wrong with the specific control you use. You could always try validating the specific control content in javascript space, regardless of ASP.NET... – Pantelis Natsiavas Nov 15 '13 at 07:59
0

I think you should rather use the RequiredFieldValidator which matches empty/non-empty content. Other validators just ignore empty content as it sounds like you hit this feature here.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
  • oh, I've used RequiredFieldValidator already, but it seem FreeTextBox control is the third-party (some one tell me this) so RequiredFieldValidator doesnt show the ErrorMessage. NOw I have to use both RequiredFieldValidator and RegularExpressionValidator for FreeTextbox control – vyclarks Nov 15 '13 at 07:14
0

Try this:

First solution:

^((?!\s).)*$

Like this:

.... ValidationExpression="^((?!\s).)*$" ....

Second solution:
you can use label instead of regularExpressionValidator control then use following code in button:

Match s = Regex.Match(TextBox1.Text, @"^((?!\s).)*$");
if (!s.Success)
{
     Label1.Text = "Incorrect input!";
}
Samiey Mehdi
  • 9,184
  • 18
  • 49
  • 63