I have been trying to use regex but I cant seem to get it to work. I’m trying to use regex with the asp.net RegularExpressionValidator. What I want it to do is to basically disallow leading and trailing spaces only.
So,
"hello " // would not work.
"hello my name is" // would work.
But in all my attempts it says bad input no matter what I put in.
Here is what I used:
^\s[a-z]+\s$
Can someone please provide one that works?
And also, what does it mean when someone says the regex returned a match?
Edit:
I have tried all the solutions in the thread and none of them work for me. Maybe it is something else wrong and not the regex? Heres is the relevant code:
<InsertItemTemplate>
<asp:TextBox CssClass="tbUpdateSummary" TextMode="MultiLine" Text='<%# Bind("updateSummary") %>'
ID="tbUpdateSummaryInsert" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="valUpdateSummaryInsert1" runat="server" Display="None"
ControlToValidate="tbUpdateSummaryInsert" ErrorMessage="Update summary must not be empty.">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ValidationExpression="^\s+|\s$" ID="valUpdateSummaryEdit2"
runat="server" Display="None" ControlToValidate="tbUpdateSummaryInsert" ErrorMessage="Update summary must not contain leading or trailing spaces."> </asp:RegularExpressionValidator>
<%--Put c# code to validate length (max 200)--%>
<asp:CustomValidator></asp:CustomValidator>
</InsertItemTemplate>
^[^\s].+[^\s]$ <======= This one actually works!!
Try this regex it should match everything that don’t have a white space at the start and end