Credit Card

The System.ComponentModel.DataAnnotations.CreditCardAttribute maps to the creditcard validation method in jQuery Validation.

Here's the model, note that the CreditCard attribute decorates the sole property on the model:

    using System.ComponentModel.DataAnnotations;

    namespace jQuery.Validation.Unobtrusive.Native.Demos.Models
    {
        public class CreditCardModel
        {
            [CreditCard]
            public string CreditCard { get; set; }
        }
    }
        

Here's the view (which uses the model):

    @model jQuery.Validation.Unobtrusive.Native.Demos.Models.CreditCardModel
    @using (Html.BeginForm())
    {
        <div class="row">
            @Html.LabelFor(x => x.CreditCard, "Only credit cards can be entered:")
            @Html.TextBoxFor(x => x.CreditCard, true)
        </div>

        <div class="row">
            <button type="submit" class="btn btn-default">Submit</button>
            <button type="reset" class="btn btn-info">Reset</button>
        </div>
    }
        

Here's the HTML that the view generates:

    <form action="/Demo/CreditCard" method="post">
        <div class="row">
            <label for="CreditCard">Only credit cards can be entered:</label>
            <input data-msg-creditcard="The CreditCard field is not a valid credit card number." data-rule-creditcard="true" 
                id="CreditCard" name="CreditCard" type="text" value="" />
        </div>
        <div class="row">
            <button type="submit" class="btn btn-default">Submit</button>
            <button type="reset" class="btn btn-info">Reset</button>
        </div>
    </form>
        

Here's the JavaScript that initialises the validation:

  $("form").validate({
      submitHandler: function (form) {
          alert("This is a valid form!");
      }
  });