Solved

Regular Expression Validator not working correctly

Posted on 2008-10-13
8
2,968 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…

778 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