Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am sure everybody has had his/her share of regex woes.

We have a big application with around 30 developers working on a mvc3 web application , and the two fold regex validations (front-end and server side) is becoming a nightmare to maintain.

Is there a way to keep all the regexes in one place ? so that they can be used as attributes for entities and also be used for validations in the controller?

share|improve this question
    
Could you maybe copy or generate both from a single source automatically during build? –  Péter Török Nov 22 '11 at 12:07
    
could you elaborate a bit more ? –  Shakehar Nov 22 '11 at 12:49
1  
My idea was, since it would be awkward to generate / reuse client side code from server side code, or vice versa, maybe it would be simpler to generate both from a third source. You could store the regexes in a plain text file, a DB, ... then generate the client and server side sources during the build from the stored regex data. Since I have no idea how the regexes are contained in your source code currently, I can't be more concrete than this. –  Péter Török Nov 22 '11 at 13:21
add comment

4 Answers 4

up vote 1 down vote accepted

What are you using for validation now? Generally, the default, built-in DataAnnotations do just what you are describing. Another option is FluentValidation which also works with client-side validation.

In any case this is a solved problem in general.

share|improve this answer
add comment

You could store the regexes in a table in your database, with a name a the key and then the two variants for front-end and back-end use, soemthing like:

regex_name | front_end                       | back end
-------------------------------------------------------------------------------
post_code  |"[a-zA-Z]\d[a-zA-Z] \d[a-zA-Z]\d"|"[a-zA-Z]\d[a-zA-Z] \d[a-zA-Z]\d"

Of course, putting this in the database could make things difficult if you need a regex before you're connected to the database (probably won't happen in this case, but you never know) so you could implement a similar idea with a static class that just has a dictionary of names and the text for the regex versions.

Or you could store this in a CSV file and then load it into a cache at application startup. There are many ways to do this, I'm not sure what is the best for your team.

share|improve this answer
    
My suggestion as well. I've done this with a database as well as in a XML file. –  jfrankcarr Nov 22 '11 at 14:45
add comment

you are talking about shared regex`s for JavaScript (ECMA) and C# (NET) correct? this is not possible because .NET and ECMA use different flavors, see Regular Expression Flavor Comparison for more info.

share|improve this answer
add comment

You can put the regex strings in your web config and call they dynamically in your page and your code. In this manner the strings will exist in a single location, will be able to be modified with out requiring a recompile, and will be available to your code through out your application.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.