Solved

Regular Expression Validator not working correctly

Posted on 2008-10-13
8
2,973 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:OVC-it-guy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 8

Accepted Solution

by:
tiagosalgado earned 250 total points
ID: 22705745
Change your regular expression to this one
^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$
0
 

Author Comment

by:OVC-it-guy
ID: 22705885
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
 
LVL 8

Expert Comment

by:tiagosalgado
ID: 22706008
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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:OVC-it-guy
ID: 22706207
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
 

Author Comment

by:OVC-it-guy
ID: 22706278
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
 

Author Comment

by:OVC-it-guy
ID: 22706450
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
 
LVL 3

Assisted Solution

by:JunkMan
JunkMan earned 250 total points
ID: 22707524
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
 

Author Comment

by:OVC-it-guy
ID: 22708174
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question