Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 3 - Compare Attribute - Perform case insenstive comparision on Client side

In the page I'm developing I have a "Email" field and "ConfirmEmail" field. And the requirement is to have a case insensitive comparision.

I could create a custom attribute and extend the behaviour of 'Compare' attribute that is inbuilt. This works on the serverside.

But I was not able to achieve it on client side. I'm sure that we have to do some additional things to make the unobtrusive jquery to do a case insensitive comparision.

like image 262
user979737 Avatar asked Apr 02 '12 18:04

user979737


2 Answers

You can use the compare attribute in MVC 3...which is a built in solution...

    [Compare("Email",ErrorMessage="your error message")]
    public string ConfirmEmail { get; set; }

Update: my bad probably I should have read your question better...anyways... for the unobtrusive way to work, after creating an attribute (the override version of Compare)... you need to do some javascript work for the unobtrusive client side validation to work...here is an example blog post unobtrusive client side validation with MVC 3 ...that does something similar to what I'm talking about...if you need further help...just ping back...I will be glad to help you with this...

Here is a more relevant post...which also talks about creating a custom attribute... Creating Custom Validation Attribute (Server side and Client Side)

Hope this helps...

like image 122
NiK Avatar answered Oct 18 '22 04:10

NiK


I'm not entirely certain what you are looking for as far as the Compare attribute, but for the JavaScript, this will do the comparison and you can take action from the client based on the results.

if (email.toUpperCase() == confirmEmail.toUpperCase()) {
    alert("Emails are a match!");        
} else {
    alert("Emails do not match");
}
like image 29
saluce Avatar answered Oct 18 '22 06:10

saluce