Solved

Regular Expression validations in .net

Posted on 2012-03-21
9
709 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
 
LVL 13

Expert Comment

by:Carl Bohman
ID: 37747834
Correction: ...both a space and a hyphen...
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 74

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now