?
Solved

Regular Expression validations in .net

Posted on 2012-03-21
9
Medium Priority
?
736 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:Frank White
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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 400 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 1600 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: 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

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 …
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
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…
Suggested Courses

850 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