Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The custom validator is working fine on server side but not working on the client side. i have a jquery script for creating adapter but still its not working below are the complete code files of the model, view and the validator i have not included controller since its not required.
Custom Validator:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace ASPNET_MVC3_Custom_ModelValidation.CustomizedValidators
{

    public class AddressAttribute : ValidationAttribute
    {

        public string _address;
        public AddressAttribute(string address)
        {
            _address = address;

        }


        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value != null)
            {
                String data = value.ToString();
                if (!data.ToLower().EndsWith("street"))
                {
                    return new ValidationResult(this.FormatErrorMessage("Address must contain street"));
                }
            }
            return null;
        }
    }


    public class AddressValidator : DataAnnotationsModelValidator<AddressAttribute>
    {
        int Address = 0;
        string errorMsg = string.Empty;

        public AddressValidator(ModelMetadata metadata, ControllerContext context, AddressAttribute attribute)
            : base(metadata,context,attribute)
        {
            errorMsg = attribute.ErrorMessage;
        }


        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
            ModelClientValidationRule validationRule = new ModelClientValidationRule();
            validationRule.ErrorMessage = errorMsg;
            validationRule.ValidationType = "address";

            validationRule.ValidationParameters.Add("address",Address);

            return new[] {validationRule };

        }
    }
}


Model class:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using ASPNET_MVC3_Custom_ModelValidation.CustomizedValidators;

namespace CValidation.Models
{
    public class Reg
    {
        [Key]
        public int Myid { get; set; }
        [Required(ErrorMessage="Username must be atleast 3 char")]
        [StringLength(10,ErrorMessage="Username must be atleast 3 char",MinimumLength=3)]
        public String Username { get; set; }
        [Required(ErrorMessage="Password must be atleast 6 char")]
        [StringLength(10,ErrorMessage="Password must be atleast 6 char",MinimumLength=6)]
        public String Pass { get; set; }
        [Required(ErrorMessage="Confirm Password must be atleast 6 char")]
        [StringLength(10, ErrorMessage = "Confirm Pass must be atleast 6 char", MinimumLength = 6)]
        public String ConfrmPass { get; set; }
        [Required(ErrorMessage = "Address required")]
        [Address("Street",ErrorMessage="Address must contain street")]
        public String Address { get; set; }
    }
}


Javascript file:
JavaScript
jQuery.validator.addMethod('requirediftrue', function (value, element, params) {
    var checkboxId = $(element).attr('data-val-requirediftrue-boolprop');
    var checkboxValue = $('#' + checkboxId).first().is(":checked");
    return !checkboxValue || value;
}, '');

jQuery.validator.unobtrusive.adapters.add('requirediftrue', {}, function (options) {
    options.rules['requirediftrue'] = true;
    options.messages['requirediftrue'] = options.message;
});
Posted
Updated 13-Nov-13 3:14am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900