Regular Expression Validator not working correctly

I am developing an ASP.NET web form in VB.NET.  For each of the form fields, I have a Regular Expression Validator.  No errors when I run the page, but as soon as I enter anything into the field, the Regular Expression Validator triggers and give my error message.  I'm only entering my first name (no spaces, no punctuation, no special characters), yet it gives the error.  What is wrong?
<asp:Label Text="First Name: " runat="server" AssociatedControlID="txtFirstName" />
<asp:TextBox ID="txtFirstName" TabIndex="1" MaxLength="50" runat="Server" ToolTip="Please type in your first name." />
<asp:RequiredFieldValidator runat="server" ID="FirstNameRequired" ControlToValidate="txtFirstName" errormessage="Please enter your first name." display="Dynamic" />
<asp:RegularExpressionValidator ID="FirstNameRegex" runat="server" ControlToValidate="txtFirstName" ValidationExpression="/^[\w-\.,\s]+$/" ErrorMessage="Invalid name - you must enter only alphabetic characters into the First Name field." Display="Dynamic" ForeColor="#FF0000" />

Open in new window

OVC-it-guyAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
tiagosalgadoConnect With a Mentor Commented:
Change your regular expression to this one
^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$
0
 
OVC-it-guyAuthor Commented:
That worked.  But when I use the same Regular Expression for the txtLastName field, and I type in my last name, then tab to the third field, the second field shifts position slightly - as if displaying an error message, but nothing is there.  May I ask a couple of variations for other fields?  I assume I would just remove the \'\,\.\- if I didn't want to allow those characters.  And what if I wanted to allow numbers or # ?
0
 
tiagosalgadoCommented:
I don't understand the "the second field shifts position slightly". Can you take a screenshot so i cant get what you're saying ?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
OVC-it-guyAuthor Commented:
You bet.  Two screen shots on the same image.  First is starting position with columns lined up the way I want them.  The second is after I type something into the txtLastName field and tab (or click) into the third field, Company.
RegEx.jpg
0
 
OVC-it-guyAuthor Commented:
And here's the code for the table displaying the form fields.
<tr><td align="left" colspan="4"><strong>Customer / Billing Information</strong></td></tr>
                    <tr>
                        <td align="right"><font color="#FF0000"><strong>*</strong></font><asp:Label Text="First Name: " runat="server" AssociatedControlID="txtFirstName" /></td>
                        <td align="left"><asp:TextBox ID="txtFirstName" TabIndex="1" MaxLength="50" runat="Server" ToolTip="Please type in your first name. It can be two names separated by a single space (ex. Mary Ann)." />
                        <asp:RequiredFieldValidator runat="server" ID="FirstNameRequired" ControlToValidate="txtFirstName" errormessage="Please enter your first name." display="Dynamic" />
                        <asp:RegularExpressionValidator ID="FirstNameRegex" runat="server" ControlToValidate="txtFirstName" ValidationExpression="^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$" ErrorMessage="Invalid name - you must enter only alphabetic characters into the First Name field." Display="Dynamic" ForeColor="#FF0000" /></td>
                        <td align="right"><font color="#FF0000"><strong>*</strong></font><asp:Label ID="lblLastName" Text="Last Name: " Runat="Server" AssociatedControlID="txtLastName" /></td>
                        <td align="left"><asp:TextBox ID="txtLastName" TabIndex="2" MaxLength="50" runat="Server" ToolTip="Please type in your last name." />
                        <asp:RequiredFieldValidator runat="server" ID="LastNameRequired" ControlToValidate="txtLastName" errormessage="Please enter your last name." display="Dynamic" />
                        <asp:RegularExpressionValidator id="LastNameRegex" runat="server" ControlToValidate="txtLastName" ValidationExpression="^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$" ErrorMessage="Invalid name - you must enter only alphabetic characters, - or ' into the Last Name field." Display="Dynamic" ForeColor="#FF0000" /></td>
                    </tr>
                    <tr>
                        <td align="right"><asp:Label ID="lblCompany" Text="Company: " Runat="Server" AssociatedControlID="txtCompany" /></td>
                        <td align="left" colspan="3"><asp:TextBox ID="txtCompany" TabIndex="3" MaxLength="50" runat="Server" ToolTip="Please type in your company name." />
                        <asp:RegularExpressionValidator id="CompanyRegex" runat="server" ControlToValidate="txtCompany" ValidationExpression="^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$" ErrorMessage="Invalid company name - please enter only alpha-numeric characters." Display="Dynamic" ForeColor="#FF0000" /></td>
                    </tr>
                    <tr>
                        <td align="right"><font color="#FF0000"><strong>*</strong></font><asp:Label ID="lblAddress1" Text="Address: " Runat="Server" AssociatedControlID="txtAddress1" /></td>
                        <td align="left" colspan="3"><asp:TextBox ID="txtAddress1" TabIndex="4" MaxLength="26" runat="Server" ToolTip="Please type in your address - 26 character limit. Use next line, if necessary." />
                        <asp:RequiredFieldValidator runat="server" ID="Address1Required" ControlToValidate="txtAddress1" errormessage="Please enter your address." display="Dynamic" ForeColor="#FF0000" />
                        <asp:RegularExpressionValidator id="Address1Regex" runat="server" ControlToValidate="txtAddress1" ValidationExpression="^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$" ErrorMessage="Invalid address - please enter only alpha-numeric characters, space, - or #" Display="Dynamic" ForeColor="#FF0000" />
                        <asp:Label ID="lblAddress1A" Text="Limit 26 characters.  You may abbreviate, but no punctuation." runat="server" /></td>
                    </tr>

Open in new window

0
 
OVC-it-guyAuthor Commented:
So I have your regular express ion down to ^[a-zA-Z]+(([\'\- ][a-zA-Z ])?[a-zA-Z]*)*$ (removed the punctuation).  What if I want to allow numbers and # ?
0
 
JunkManConnect With a Mentor Commented:
this would do it ^[A-Za-z0-9\s#-]+$ you can confirm it works here http://www.regextester.com/

about the rendering issue, have you tried moving the validators and see if the same behaviour occurs?
0
 
OVC-it-guyAuthor Commented:
It did do.  Thanks.  Yes, I was already working the rendering.  Moving it (after a <br> following the asp:textbob,within the same <td></td> did well for me.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.