Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regular Expression Validator not working correctly

Posted on 2008-10-13
8
Medium Priority
?
2,975 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 1000 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
Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

 

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 1000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
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…

715 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