Solved

Regular Expression validations in .net

Posted on 2012-03-21
9
719 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
[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
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…
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