Blog Archives

MVC Regex Phone Numbers and Emails

This is just a quick bit on Phone Numbers and Emails in MVC.  Well, at least MVC3.  The [DataType(DataType.PhoneNumber, ErrorMessage = “You must provide a valid phone number.”)] and [DataType(DataType.EmailAddress, ErrorMessage = “You must provide a valid email address.”)] apparently does not perform the actual validation for you.  Don’t fret though, you can add the RegularExpression annotation to it.

Here are two examples, you can use any regex expression, these worked for my needs.  I also left some other annotations in there.

[Required(AllowEmptyStrings = false, ErrorMessage = "You must provide a supervisor email.")]
[StringLength(50, ErrorMessage = "A supervisor email cannot exceed 50 characters.")]
[Display(Name = "Supervisor Email")]
[DataType(DataType.EmailAddress, ErrorMessage = "Please enter a valid supervisor email address.")]
[RegularExpression("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$", ErrorMessage = "You must provide a valid supervisor email address.")]
public string SupervisorEmail { get; set; }

[Required(AllowEmptyStrings = false, ErrorMessage = "You must provide your home phone number.")]
[Display(Name = "Home Phone")]
[DataType(DataType.PhoneNumber, ErrorMessage = "You must provide a proper phone number.")]
[RegularExpression("^\\D?(\\d{3})\\D?\\D?(\\d{3})\\D?(\\d{4})$", ErrorMessage = "You must provide a proper phone number.")]
public string HomePhone { get; set; }
Advertisements

MVC3 Custom Validation

I’m still trying to find the time to truly learn MVC.  I know I like it better than web forms, it is just a matter of spending the quality time with it at home and at work to get as comfortable with it as I am with web forms.  One area that I could not find the answer to, and kept pushing back for later, is custom validation.  I found many examples of doing custom validation where you hard code some value, but that isn’t what I wanted.  I wanted to do custom validation based on a field being unique in a database and have it work with the DataAnnotations and the whole Html.ValidationMessageFor in Razor.

So, after spending some time focused on it I figured it out.  It really isn’t that hard at all.  In a nutshell you create a new class that implements the ValidationAttribute; this class will do the actual work.  Then you add your new attribute to your model.  Of course I’ll include a simple example.

Assumptions of my example.  I have a table called tblTitle, it contains id, name, and isValid fields.  My project uses Entity Framework.  I create a class to handle my business logic and a class that will act like my model.  I also decorate my model class with appropriate DataAnnotations.  I have a controller, which checks for the model state to be valid before calling the business logic’s update or create method. My business logic class also contains the method to check for a unique name.

We want our name field in the title table to be unique inside that table.  No need to have multiple versions of the same title right?

Read the rest of this entry

Are Leading Zeros Required For Range Validation

The quick answer to this, NO. If you have a range validator that appears to want you to have leading zeros chances are you made a bone headed mistake. Much like I did just now. I forgot to set the Type property of my range validator which gave me some interesting results.

The below range validator required me to enter 01 – 100. Another odd thing about this mistake was that a value of 43, had to be 043.

<asp:RangeValidator ID="valrngTest" runat="server" ControlToValidate="txtTest" 
    Text="*" ErrorMessage="Test must be between 0 - 100." SetFocusOnError="true" 
    Display="Dynamic" MinimumValue="0" MaximumValue="100">
</asp:RangeValidator>

This one worked just as you would expect. Notice the addition of the Type=”Integer”.

<asp:RangeValidator ID="valrngTest" runat="server" ControlToValidate="txtTest" 
    Text="*" ErrorMessage="Test must be between 0 - 100." SetFocusOnError="true" 
    Display="Dynamic" MinimumValue="0" MaximumValue="100" Type="Integer">
</asp:RangeValidator>

Custom Validation Controls

Custom validation controls are a nice tool that you can use when the standard validation controls just won’t do the job.  Most examples I found out there only addressed the server side validation aspect.  Don’t get me wrong, you can do that, just use server side, but ideally you want to do both.

Trust me though, if you only want to do one side, do server side.  You will want to always do server side as a minimum just in case the user has javascript disabled or something.  Server side only is kind of old school but it gets the job done very reliably.

Read the rest of this entry