2

Is there a way how to perform a "database" check through the Client Side Validation in MVC?

I have the following class

public class EmailCheck : ValidationAttribute,IClientValidatable
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        string sErrorMessage = "Email already exists";
        return new ValidationResult(sErrorMessage); 
    }   

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        ModelClientValidationRule validationRule = new ModelClientValidationRule();
        validationRule.ValidationType = "checkemail";
        validationRule.ErrorMessage = "Invalid Email Format";
        validationRule.ValidationParameters.Add("param", "");
        return new List<ModelClientValidationRule> { validationRule };
    }  
}

I would like the Client Side Validation to call the "IsValid" method onkeyup/lost focus as well and not just do the regular javascript checks in the "checkemail" javascript function.

The javascript function i have is the following:

//Validation for Well-Formed Email
jQuery.validator.addMethod("checkemail",
    function (value, element, param) {
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        if (!emailReg.test(value))
            return false;

        return true;
    });

jQuery.validator.unobtrusive.adapters.add("checkemail", ["param"], function (options) {    
    options.rules["checkemail"] = options.params.param;
    options.messages["checkemail"] = options.message;
});

I would appreciate if anyone could guide me in the right direction or provide a tutorial of something similar.

Thanks

2 Answers 2

4

In your model try to use the "Remote" attribute

http://msdn.microsoft.com/en-us/library/gg508808%28v=vs.98%29.aspx

Sign up to request clarification or add additional context in comments.

6 Comments

Ok thanks, that seems to work.. however when I implement the RemoteAttribute my Client Side Validation javascript checks seem to stop working. Is this normal?
what kind of checks ? can you elaborate? RemoteAttribute lets you define the javascript function on your view to be called when someone enters the value in your textbox.
I had a custom ValidationAttribute check called [EmailCheck] which fired GetClientValidationRules that called the javascript function "checkemail" onkeyup. That function is not being called anymore, or is it possible to call that javascript function using the RemoteAttribute?
i think there is no need to call javascript function [Remote("checkemail", "controllername", AdditionalFields = "ID(the id of user)", HttpMethod = "POST", ErrorMessage = "User with same Emailaddress already exists")] The remote attribute will automatically call the action which checks for existing email in database.
No, sorry maybe I didnt explain properly. I first had a client-side validation javascript function called checkmail which was checking in regex whether the user input is in correct format. If that was successful, I wanted it to make use of the RemoteAttribute to do the necessary checks in the database. Is that possible, or should I just combine all into 1 function?
|
0

Rusty is correct, you need to use the Remote attribute in your model

Here is another example with complete detail only for email validation in MVC

MVC model validation for email from database in asp.net

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.