?
Solved

Regular Expression Validator Validation Control Expression

Posted on 2011-10-13
7
Medium Priority
?
798 Views
Last Modified: 2012-05-12
I need a RegularExpressionValidator to validate a textbox that has only one character.  It can have only the letters A-Z, or the numbers 0-9, or a dash, or an apostrophe, or a space.  Any ideas what the ValidationExpression is?
0
Comment
Question by:us1975mc
  • 3
  • 3
7 Comments
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 36962237
Try this

([A-Z]{1,1})|([0-9]{1,1})|(-)|(')|(\S)
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 36962284
Try this;

 
SubjectString = "121234567890-=\][poiuytrewqq';l/., " 
Try
	If Regex.IsMatch(SubjectString, "[\w\x20\x27\x2D]") Then
		' Successful match
	Else
		' Match attempt failed
	End If
Catch ex As ArgumentException
	'Syntax error in the regular expression
End Try

Open in new window

0
 

Author Comment

by:us1975mc
ID: 36962392
SriVaddadi,

I am still able to enter other characters.  Below is my code.


<asp:RegularExpressionValidator ID="Regex1" 
                runat="server" 
                ErrorMessage="RegularExpressionValidator" 
                ControlToValidate="Let1"
                ValidationExpression="([A-Z]{1,1})|([0-9]{1,1})|(-)|(')|(\S)" > 
            </asp:RegularExpressionValidator>

Open in new window

0
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!

 

Author Comment

by:us1975mc
ID: 36962404
mgfranz,

I am really lost on this.  Do I still need a RegularExpressionValidator?  Where do I place the code?  In the Let1_TextChanged?
Protected Sub Let1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Let1.TextChanged
        Dim Subjectstring As String
        SubjectString = "121234567890-=\][poiuytrewqq';l/., "
        Try
            If Regex.IsMatch(SubjectString, "[\w\x20\x27\x2D]") Then
                Let2.Focus()
            Else
                Let1.Focus()
                MsgBox("Try Again")
            End If
        Catch ex As ArgumentException
            'Syntax error in the regular expression
        End Try
    End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:mgfranz
ID: 36962546
If you would rather not use ASCII parameters, you can use this;

 
SubjectString = "`~1!2@3#4$5%6^7&8*9(0)-_=+qwertyuiop[{]}\|asdfghjkl;:'zxcvbnm,<.>/? "
Try
	Dim RegexObj As New Regex("[A-Za-z0-9'\- ]")
	If RegexObj.IsMatch(SubjectString) Then
		' Successful match
	Else
		' Match attempt failed
	End If
Catch ex As ArgumentException
	'Syntax error in the regular expression
End Try

Open in new window

0
 
LVL 18

Accepted Solution

by:
mgfranz earned 2000 total points
ID: 36962601
Sorry, missed your post.

You can either use a simple RegEx function like I posted, or the Validator control (like you asked for...).  Use this expression;

<asp:RegularExpressionValidator id="RegularExpressionValidator1"
    ControlToValidate="TextBox1"
    ValidationExpression = "[A-Za-z0-9'\- ]"
    Display="Static"
    EnableClientScript="false"
    ErrorMessage="Invalid character found" runat="server"/>
0
 

Author Closing Comment

by:us1975mc
ID: 36965401
This is a solution for what I have ask but I guess I ask the wrong question.  I will post another question tomorrow dealing with a different twist.

Thank you for your time and effort.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

830 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