Solved

Regular Expression validations in .net

Posted on 2012-03-21
9
714 Views
Last Modified: 2012-03-26
Experts,

I have a problem..I need to allow following characters (  space, hyphen -, forward slash /, back slash\, apostrophe'   ) in my textbox and validations has to pass. I achieved through regular expressions. Whether these special characters occurs single time or multiple times.

<RegularExpressionValidator ValidationExpression="^([a-zA-Z0-9,\#])*([a-zA-Z0-9,\-\.\s\#\|\'\\])*$" ErrorMessageID="Validate.MyProfile.Address1.ValidCharacters" />

Open in new window


However, the problem is..I have to allow all these characters only one time in the textbox. Multiple occurences are NOT allowed in the future. Please help me fix this problem.

Thanks
0
Comment
Question by:SubbuUSA
9 Comments
 
LVL 3

Expert Comment

by:DaFranker
ID: 37747750
Hmm. If I understand this correctly, isn't it as simple as declaring a boolean variable and adding a regexp search for your validated characters? If the added regexp finds a match, set the bool to true, and then add a check for that bool before the validation. If false, do the above validation, else, do another validation that rejects those characters. If needed, save the bool to some file or registry entry.

Or am I misunderstanding the problem?
0
 

Author Comment

by:SubbuUSA
ID: 37747812
@ DaFranker

Existing architecuture validates only through xml files having regular expressions for some reasons. No code behind is allowed in my architecture to validate.

Is there a way,  I can fix this by writing a regex code like the one which I have provided that do this restriction? Any help would be appreciated
0
 
LVL 13

Expert Comment

by:Carl Bohman
ID: 37747821
Is it that you can only allow one of those characters once or that you can allow each of them once?  For example, if a string contained both a string and a hyphen, would it be considered valid or invalid?

This seems like an odd validation requirement.  Is it for a school assignment?
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 13

Expert Comment

by:Carl Bohman
ID: 37747834
Correction: ...both a space and a hyphen...
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 100 total points
ID: 37747854
If I understand you issue correctly, try:

<RegularExpressionValidator ValidationExpression="^([a-zA-Z0-9,#])*([a-zA-Z0-9,.#|]| (?![^ ]* )|-(?![^-]*-)|/(?![^/]*/)|\\(?![^\\]*\\)*$" ErrorMessageID="Validate.MyProfile.Address1.ValidCharacters" />

Open in new window

0
 

Author Comment

by:SubbuUSA
ID: 37747874
@bounsy

Thanks for your reply.

" Is it that you can only allow one of those characters once?"

The string containing a space and hyphen is considered as valid. The combination of special characters are also valid but it can appear only once. They can be in any order but can occur only once.

No it is not for school..it is for banks
0
 
LVL 13

Accepted Solution

by:
Carl Bohman earned 400 total points
ID: 37747985
It sounds like you can have multiple special characters in a single cluster with no other clusters of special characters and no duplicate special characters in that cluster.  If so, this should work:
<RegularExpressionValidator ValidationExpression="^[a-zA-Z0-9,\#]*(?!([\-\.\s\|\'\\]).*\1)[\-\.\s\|\'\\]*[a-zA-Z0-9,\#]*$" ErrorMessageID="Validate.MyProfile.Address1.ValidCharacters" />

Open in new window

If, however, multiple clusters of special characters are allowed, then this is the regex to use:
<RegularExpressionValidator ValidationExpression="^(?!.*([\-\.\s\|\'\\]).*\1)[a-zA-Z0-9,\#]*(?:[\-\.\s\|\'\\][a-zA-Z0-9,\#]*)*$" ErrorMessageID="Validate.MyProfile.Address1.ValidCharacters" />

Open in new window

0
 

Assisted Solution

by:SubbuUSA
SubbuUSA earned 0 total points
ID: 37749374
@kaufmed
Your code does not fire any validation messages to me. May be something is wrong..could not find what is causing that.

@bounsy

I do not understand what do you mean by clusters but your code did not work for me.

I tried using your first line of code..it does not allow me even type hyphen , slash one time..it throws up the error message..

I am supposed to allow space, hyphen, slashes both, apostrophe one time in the textbox and show throw if it occurs more than one time. regardless of what sequence the characters and special characters may appear. it does not matter.


Your second line of code fixes my problem. Thanks again for helping me.
thanks
0
 

Author Closing Comment

by:SubbuUSA
ID: 37764973
Thanks Kaumed and Bounsy for saving my day..you have a great day
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Adding bootstrap to only Content page and NOT master page 1 47
VB.NET 2008 Winforms Signing 13 30
Finding the IIS version 5 21
Code enhancement 4 20
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…

789 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