Something wrong with RegularExpressionValidator

192 views Asked by At

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).

3

There are 3 answers

1
Wiktor Zychla On

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.

4
Pantelis Natsiavas On

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

Hope I helped!

0
Samiey Mehdi On

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