Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Regular Expression validations in .net

Posted on 2012-03-21
9
Medium Priority
?
733 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
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…

636 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