D
D
dsoloma342020-05-08 13:19:08
JavaScript
dsoloma34, 2020-05-08 13:19:08

How can I prevent the form from submitting if there are not enough characters?

It is necessary that the form is not submitted if the input type="tel" is less than 15 characters.

Here's what's there:

<input type="tel" placeholder="+48 111-111-111" id="phone" name="phone" data-field="item" required>
 <input id="but1" value="Zamówic" type="submit">

$(function () {
   var inputName = $('[data-field="input-name"]');
   $(document).on('input', '[data-field="item"]', function () {
    var item = $(this);
    
    $('#but1').on('click', function() {
        if (item.val().length < 15) {
          alert('< 15 символов');
        }
    });
  });
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Pastukhov, 2020-05-08
@dsoloma34

$('#but1').on('click', function(event) {
  if (item.val().length < 15) {
    alert('< 15 символов');
    event.preventDefault();
    // event.stopPropagation(); <-- еще можно остановить клик, но так не советуется делать
  }
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question