Solved

Regular Expression Validator not working correctly

Posted on 2008-10-13
8
2,969 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
  • 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

820 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