parseInt with substr do not detect type on input

(index):34 Uncaught TypeError: Cannot read property ‘length’ of null

So the main goal i when write on textarea count on status div. I saw on many tutorial use with it parseInt and substr for get the number string from to.

Any idea why not work? And also what wrong with error above?

Thanks!

Your link doesn’t work.

1 Like
  var type = $(this).val().match(/\d/g).length.substr(0, limit);

When $(this).val() does not contain a number, $(this).val().match(/\d/g) is null, so you can’t get its length.
When it does contain a number, $(this).val().match(/\d/g).length is a number and you can’t take a substring of it.

1 Like

@design Not sure exactly what you are trying to do, but if you want to show the difference in the maxlength attribute value of the textarea and how many numbers that have been typed, you could do:

$('textarea').keyup('keypress', function() {
  var limit = parseInt($(this).attr('maxlength'));
  var numbersMatch = $(this).val().match(/\d/g);
  var numbersTyped = numbersMatch && numbersMatch.length;
  $('#status')
    .html(`<small>You have ${limit - numbersTyped} typed numbers remaining.</small>`)
    .fadeIn(500, 'linear');
});
1 Like

${limit - numbersTyped}
Is a new style? for paste var? Other than '+ var +'
?

Thanks!