2

I want to use jQuery validation for credit card input fields. When I seach for it I saw most people made this happen with additional validation methods.

I tried adding this to my code :

<script src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js"></script>

or this code :

jQuery.validator.addMethod("lettersonly", function(value, element) {
  return this.optional(element) || /^[a-z]+$/i.test(value);
}, "Letters only please"); 

But I'm getting this error

enter image description here

Is this about a version problem, I use the jQuery 3.6 version and tried 1.7 and 1.9 versions for validation plugin.

Note: I'm sure that I use validation plugin after adding jQuery cdn. I can reach jQuery but not to jQuery.validator method.

Addition: I'm tring to format name-surname input field with only letters and spaces. If you can help me with that too, I would be glad.(Or is there a plugin prepared for every input field for credit card inputs.)

Edit:

The form I'm using right now.

<form id="cardForm">
    <div class="input-group">
        <div class="card-input-row row w-100 mt-5">
            <div class="col-12">
                <label for="card-number" class="mb-1 input-group-label">Card Number</label>
                <input id="card-number" pattern="\d*" maxlength="19" type="text" placeholder="••••  ••••  ••••  ••••" class="form-control w-100 shadow mb-3 input-group-inputs">
            </div>
            <div class="col-12">
                <label for="cardName" class="mb-1 input-group-label">Name</label>
                <input id="cardName" type="text" placeholder="••••••••  ••••••••" class="form-control w-100 shadow input-group-inputs mb-3">
            </div>
            <div class="row" style="padding-right: 0px ">
                <div class="col-5" style="padding-right: 0px; padding-right: 0px;">
                    <label for="card-date" class="mb-1 input-group-label">Expiration Date</label>
                    <input id="card-date" type="text" placeholder="MM/YY" class="form-control w-100 shadow input-group-inputs">
                </div>
                <div class="col-2"></div>
                <div class="col-5" style="padding-right: 0px; padding-right: 0px;">
                    <label for="card-cvc" class="mb-1 input-group-label">CVC</label>
                    <input id="card-cvc" type="text" placeholder="•••" class="form-control w-100 shadow input-group-inputs">
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-6">
                <button class="btn btn-primary">Submit</button>
            </div>
        </div>
    </div>
</form>

And the jquery example given added like:

$('#cardForm').validate({
    rules: {
        cardName: {
            required: true,
            lettersonly: true,
        },
    },
});

It does not give any errors but it doesn't work either.

AsIndeed
  • 138
  • 1
  • 13

1 Answers1

2

I tried on below order and seems everything is ok wihtout any problem:

$("#cardName").on('input',function(event){
  const value = $(this).val();
  if (/^[A-Za-z]+$/gi.test(value) == false) {
    $(this).val(value.slice(0, -1));
  }
})

var month = 0;
$("#card-date").on('keypress',function(event) {
  if (event.charCode >= 48 && event.charCode <= 57) {
    if ($(this).val().length === 1) {
      $(this).val($(this).val() + event.key + "/");
    } else if ($(this).val().length === 0) {
      if (event.key == 1 || event.key == 0) {
        month = event.key;
        return event.charCode;
      } else {
        $(this).val(0 + event.key + "/");
      }
    } else if ($(this).val().length > 2 && $(this).val().length < 5) {
      return event.charCode;
    }
  }
  return false;
}).on('keyup',function(event) {
  if (event.keyCode == 8 && $("#card-date").val().length == 2) {
    $(this).val(month);
  }
})
<form id="cardForm">
  <div class="input-group">
    <div class="card-input-row row w-100 mt-5">
      <div class="col-12">
        <label for="card-number" class="mb-1 input-group-label">Card Number</label>
        <input id="card-number" pattern="\d*" maxlength="19" type="text" placeholder="••••  ••••  ••••  ••••" class="form-control w-100 shadow mb-3 input-group-inputs">
      </div>
      <div class="col-12">
        <label for="cardName" class="mb-1 input-group-label">Name</label>
        <input id="cardName" name="cardName" type="text" placeholder="••••••••  ••••••••" class="form-control w-100 shadow input-group-inputs mb-3">
      </div>
      <div class="row" style="padding-right: 0px ">
        <div class="col-5" style="padding-right: 0px; padding-right: 0px;">
          <label for="card-date" class="mb-1 input-group-label">Expiration Date</label>
          <input id="card-date" type="text" placeholder="MM/YY" class="form-control w-100 shadow input-group-inputs">
        </div>
        <div class="col-2"></div>
        <div class="col-5" style="padding-right: 0px; padding-right: 0px;">
          <label for="card-cvc" class="mb-1 input-group-label">CVC</label>
          <input id="card-cvc" type="text" placeholder="•••" class="form-control w-100 shadow input-group-inputs">
        </div>
      </div>
    </div>
    <div class="row">
      <div class="col-6">
        <button class="btn btn-primary">Submit</button>
      </div>
    </div>
  </div>
</form>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.19.1/additional-methods.min.js"></script>
Saeed Shamloo
  • 6,199
  • 1
  • 7
  • 18
  • 1
    I tought just "" was enough for adding validation settings. After adding cdn for jquery.validation it worked perfectly fine. Thank you so much. However I still can't figure out lettersonly rule addition. – AsIndeed Dec 10 '21 at 06:55
  • 2
    I edited my answer, could please check it. – Saeed Shamloo Dec 10 '21 at 07:27
  • 1
    I think the problem is related to the version of `additional-methods` you have been used in your project, I used another version and `lettersonly` validation worked like a charm. – Saeed Shamloo Dec 10 '21 at 08:10
  • 2
    I edited my question. It doesn't give any errors but won't work either. – AsIndeed Dec 10 '21 at 08:47
  • 1
    I copied your form and paste it in my answer, the problem with your form was that you didn's set name for cardName field. check the result of my asnwer again please. – Saeed Shamloo Dec 10 '21 at 08:50
  • 2
    Please consider the point that `jquery.validate` works with the name of fields not their Ids. – Saeed Shamloo Dec 10 '21 at 08:53
  • 1
    Again my beginner brain tought giving id to an input was enough.. That solved my problem and the validation works fine thank you. However I found out this validation is not the one I'm looking for. I want a format that user can't even place a number or special char to that input. Not just a warning that says only letters please. – AsIndeed Dec 10 '21 at 08:56
  • 2
    I edited my response again, but I just prevent typing any characted except letters in the `cardName` field, I don't what is your pattern exactly – Saeed Shamloo Dec 10 '21 at 09:09
  • 2
    It was the least I could do. – Saeed Shamloo Dec 10 '21 at 10:31
  • 1
    Hello Saeed, now I'm trying to format date area with mm/yy format. I started with your refineValue method but it was a real challange for me, I just couldn't 100% figured it out. Could you please help me with that? – AsIndeed Dec 11 '21 at 12:17
  • 2
    @AsIndeed Hello, Sure, what is the problem? could you please explain it? – Saeed Shamloo Dec 11 '21 at 12:54
  • 1
    Well, I need a format for credit card expiration date input like you did for name input. The format is mm/yy, ex: 11/25. I tried to write a function for it but "/" in the middle makes things very hard. – AsIndeed Dec 11 '21 at 13:30
  • 2
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/240042/discussion-between-saeed-shamloo-and-asindeed). – Saeed Shamloo Dec 11 '21 at 13:40
  • 2
    @AsIndeed Hello again, last night I spent several hours on the expiration date and implemented a simple one and put it in the answer, I hope this help you. – Saeed Shamloo Dec 12 '21 at 05:13
  • 1
    I don't know how to thank you. I had busy today so I couldn't work on it. But I came home and see your answer is working perfectly. You made my day.. – AsIndeed Dec 12 '21 at 19:49
  • 2
    @AsIndeed Glad to help. – Saeed Shamloo Dec 12 '21 at 20:07