0

Javascript to accept 16 characters(all numbers), not less or not more

<div class="col-xs-4">
<div style="text-align:left;" class="form-group">
<label for="credit0_number">Credit Card Number</label>
<input type="number" class="inp form-control" id="credit0_number" name="credit0[number]" >
</div>
 </div>

I tried maxlength = '16' but it did not work I also did min="16" max = "16" , it didnt work as well what can I do?

sarah
  • 87
  • 9

6 Answers6

6

maxlength only limits the length of the input to 16 characters. Min and max are only available for input type number and they limit the actual value, not the length of the value (length of the string).

I believe you are trying to validate a credit card? You could use the pattern attribute and a regular expression:

<input type="text" pattern="[0-9]{13,16}" required>

This means any number containing the digits 0-9 and 13-16 chars in length. If you only want to allow 16 chars, try this:

<input type="text" pattern="[0-9]{16}" required>
HaukurHaf
  • 13,522
  • 5
  • 44
  • 59
  • It enters letters, I only want numbers that why I have type = "number" – sarah May 24 '17 at 13:47
  • No it just let me enter letters – sarah May 24 '17 at 13:48
  • Yeah but you cannot submit the value. – HaukurHaf May 24 '17 at 13:50
  • Can it show an error on the spot? without having to submit the form yet? – sarah May 24 '17 at 13:51
  • @Sarah You can read more about the `pattern` attribute and the way they validate content [here](https://developer.mozilla.org/en/docs/Web/HTML/Element/Input) and [here](https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Form_validation). You may also want to read more about event driven form validation through JavaScript and even server-side validation. – Jeff Noel May 24 '17 at 13:52
  • @sarah, no not really. This is using the built in validation provided by HTML5 and it's much more limited than a custom made JS solution. For a more robust solution, check out some Javascript implementations (and don't forget to perform the validation also on the backend!). https://stackoverflow.com/questions/6176802/how-to-validate-credit-card-number – HaukurHaf May 24 '17 at 13:56
0

If these input fields are wrapped in a form, you can use an [onsubmit] listener on the form.

If you are using jQuery, you can do that like this:

$("#id-of-your-form").on("submit", function(event) {
    var len = $("#credit0_number").val().length;
    if (len != 16) {
        event.preventDefault();

        // Show an error message here
    }
});

EDIT

If you also want to check on keyup, you could do something like this:

function validCredit() {
    return $("#credit0_number").val().length == 16;
}

$("#id-of-your-form").on("submit", function(event) {
    if (!validCredit()) {
        event.preventDefault();

        // Show an error message here
    }
});

$("#credit0_number").on("keyup", function(event) {
    if (!validCredit()) {
        // Show an error message here
    }
});
Midgard
  • 382
  • 1
  • 2
  • 12
0

Addition to @HaukurHaf's answer, you need wrap your form with <form> to show the validation message.

Without tag:

<div class="col-xs-4">
  <div style="text-align:left;" class="form-group">
    <label for="credit0_number">Credit Card Number</label>
    <input type="text" pattern="[0-9]{13,16}" required>
    <input type="submit">
  </div>
</div>

With tag:

<form>
  <div class="col-xs-4">
    <div style="text-align:left;" class="form-group">
      <label for="credit0_number">Credit Card Number</label>
      <input type="text" pattern="[0-9]{13,16}" required>
      <input type="submit">
    </div>
  </div>
</form>
Community
  • 1
  • 1
Dalin Huang
  • 11,212
  • 5
  • 32
  • 49
0

If you don't want to use HTML5 attributes, you may want to have a pure JavaScript implementation using regular expressions and the input event (click and keyup are not good enough because the user can use his mouse or keyboard with an input[type="number"] ).

The pattern you want is certainly /^\d{16}$/ to be sure that there are only 16 numbers in the field.

var num = document.getElementById('num');

num.addEventListener('input', handler, false);

function handler() {
  var pattern = /^\d{16}$/;
  if (pattern.test(num.value)) {
    console.log('---> OK <---');
  } else {
    console.log('KO');
  }
}
<input id="num" type="number">
Badacadabra
  • 8,043
  • 7
  • 28
  • 49
-1

did you try to put the input inside a form?

And using a pattern works fine for me:

<input type="text" pattern="\d{16}">
Fabian Mendez
  • 512
  • 5
  • 15
-1

Add an onkeypress to allow only numbers to keyed. Then add an input pattern to only allow 16 characters.

<form>
  <div class="col-xs-4">
    <div style="text-align:left;" class="form-group">
      <label for="credit0_number">Credit Card Number</label>
      <input pattern="[0-9]{16}" type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57'>
      <input type="submit">
    </div>
  </div>
</form>
Jason
  • 268
  • 5
  • 14
  • 1
    That is fairly hackish. Users are now blocked from doing Ctrl+V to paste, and can still rightclick and paste non-numerical values. – Midgard May 24 '17 at 14:11
  • The pattern ".{16}" means "16 of anything". Later on you are testing if the pressed character is a number, so why wouldn't you simply say "16 digits", namely using the pattern "[0-9]{16}" – Pieter De Clercq May 24 '17 at 14:22
  • I can still Ctrl+V to paste and pasting non-numerical values are present for all the other solutions here too. So I'm not sure why there's a downvote. I did make an error however which I will edit. Missing numerical validation: ` – Jason May 24 '17 at 14:23
  • @Jason given that edit, the onkeypress isn't necessary anymore. – Pieter De Clercq May 24 '17 at 14:24
  • @thepieterdc Well theres a older post about this: https://stackoverflow.com/questions/469357/html-text-input-allow-only-numeric-input The onkeypress might not be necessary but I don't see a big problem with it as an addition. Nothing beats server-side validation in the end. But is there a real problem with the onkeypress? – Jason May 24 '17 at 14:27
  • @Jason ofcourse you always need serverside validation, you can never trust your user, but what I mean is that when you are using a pattern, the onkeypress wouldn't do anything I guess since the pattern would cover this – Pieter De Clercq May 24 '17 at 14:28
  • @thepieterdc as commented before in the answer post, the non-numeric value will not be submitted. But it can still be keyed. I guess in the wider schemes of things, its not necessary but it kind of simulates serverside validation. You see it often in creditcard/dates/sort-codes inputs. – Jason May 24 '17 at 14:33