3

I have a database field (postcode) that I want to contain upper case characters, spaces and numbers. No lower case or other punctuation. I'd like to deal with that as declaratively as possible. One way would be to use a regular expression DataAnnotation to validate, [A-Z][0-9]\w (or similar - I'm not a regular expression expert, but I'm sure the right regular expression is there to be found). That would do half of what I want - it would force the user to input in uppercase, but it wouldn't convert to uppercase.

If I want to convert to uppercase there are some solutions in Stack Overflow question How can I force input to uppercase in an ASP.NET textbox?, but none of them are particularly declarative. At the moment I'm thinking of a combination of the validation and jQuery to spot anything with a class of "uppercaseonly" and convert the data, but it's a bit messy.

Am I missing anything?

Community
  • 1
  • 1
Andiih
  • 12,285
  • 10
  • 57
  • 88

2 Answers2

2

CSS text-transform: uppercase?

http://www.w3schools.com/css/pr_text_text-transform.asp

UPDATE:

Try implementing a custom ModelBinder. Here's an example: http://www.agileatwork.com/custom-model-binder-in-asp-net-mvc/

Instead of AddRoles just set the property to uppercase. No need to touch your EF classes.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
  • 1
    Doesn't that just display the characters in upper case, rather than changing them to upper case ? In which case I will have mixed case stored in the backing database, on reports etc? – Andiih Jun 04 '11 at 09:36
  • Try implementing a custom ModelBinder. – Jakub Konecki Jun 04 '11 at 19:44
2

Why not just change the setter to store the received value as uppercase?

So, in your class object, instead of the shorthand property { get; set; } change to;

get { return this.property; }

set { this.property = value.ToUpper(); }
GSerg
  • 76,472
  • 17
  • 159
  • 346
pmirvine
  • 31
  • 2