Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7522375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:30:16+00:00 2026-05-30T02:30:16+00:00

Created a custom validation attribute and I would like to get it to work

  • 0

Created a custom validation attribute and I would like to get it to work on client side as well. Here is the Model class

public class CreditCardAttribute : ValidationAttribute, IClientValidatable
    {
        public override bool IsValid(object value)
        {
            var number = Convert.ToString(value);
            return IsValidNumber(number);
        }
        private bool IsValidNumber(string number)
        {
            int[] DELTAS = new int[] { 0, 1, 2, 3, 4, -4, -3, -2, -1, 0 };
            int checksum = 0;
            char[] chars = number.ToCharArray();
            for (int i = chars.Length - 1; i > -1; i--)
            {
                int j = ((int)chars[i]) - 48;
                checksum += j;
                if (((i - chars.Length) % 2) == 0)
                    checksum += DELTAS[j];
            }

            return ((checksum % 10) == 0);
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            yield return new ModelClientValidationRule { 
            ErrorMessage = this.ErrorMessage,
            ValidationType = "CreditCard"
            };

        }
    }

jQuery:

jQuery.validator.addMethod('CreditCardtest', function (value, element, params)
    {
    return false;
    });


    jQuery.validator.unobtrusive.adapters.add('CreditCard', { }, function(options){
        options.rules['CreditCardtest'] = true;
        options.messages['CreditCardtest'] = options.message;
    });

I am not sure what should go to CreditCardTest, do I want to rewrite what I have in Model to here?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:30:18+00:00Added an answer on May 30, 2026 at 2:30 am

    Validating a credit card number is probably something that you don’t want to implement client validation for.

    But in case you are interested how to achieve it in the general case you will have to implement the same logic you have on your server in javascript.

    There’s just one thing that is wrong with your custom validation attribute and this is name of the validation type which must contain only lowercase letters:

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        yield return new ModelClientValidationRule 
        { 
            ErrorMessage = this.ErrorMessage,
            ValidationType = "creditcard" // this should be only lowercase letters
        };
    }
    

    Once we have fixed that we could implement the same validation logic on the client:

    @model MyViewModel
    
    <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
    <script type="text/javascript">
        var isValidNumber = function (value, element, params) {
            var DELTAS = [0, 1, 2, 3, 4, -4, -3, -2, -1, 0];
            var checksum = 0;
            for (var i = value.length - 1; i > -1; i--) {
                var j = value.charCodeAt(i) - 48;
    
                checksum += j;
                if (((i - value.Length) % 2) == 0) {
                    checksum += DELTAS[j];
                }
            }
            return (checksum % 10) == 0;
        };
    
        jQuery.validator.addMethod('CreditCardtest', isValidNumber);
    
        jQuery.validator.unobtrusive.adapters.add('creditcard', { }, function (options) {
            options.rules['CreditCardtest'] = true;
            options.messages['CreditCardtest'] = options.message;
        });
    </script>
    
    @using (Html.BeginForm())
    {
        @Html.EditorFor(x => x.Number)
        @Html.ValidationMessageFor(x => x.Number)
        <button type="submit">OK</button>
    }
    

    Obviously all the script stuff goes into a separate file and not inside the view. I just left it here for easier readability.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a Custom Validation Attribute: public sealed class DateAttribute : DataTypeAttribute {
I would like to create a custom action filter attribute that adds a value
I have created a custom validation attribute by subclassing ValidationAttribute. The attribute is applied
I have created custom validation attribute [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited =
I have created my own custom ValidationAttribute : public class UrlValidationAttribute : ValidationAttribute {
I've created a custom textInput componenet that handles it's own validation using a private
Is this the only way to create custom model validation? To do it using
I have created custom MembershipUser, MembershipProvider and RolePrivoder classes. These all work and I
i created a custom menu called top-navigation. Now I'd like to style it with
I created a custom menu called sub-top-nav and now I'd like to override the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.