Form Code:

<form role="form" id="payment-form" onsubmit="return validate(this);"> <div class="row"> <div class="col-xs-12"> <div class="form-group"> <label>Номер</label> <div class="input-group"> <input type="text" class="form-control" name="Number" placeholder="Номер" required /> <span class="input-group-addon"> </div> </div> </div> </div> <div class="row"> <div class="col-xs-12"> <button class="btn btn-primary" id="bbankCard" type="button">Принять</button> 

However, you cannot change the <button> to <input> . As the data that I enter in the form field, send to the JS handler and check for a match with var number = new RegExp("/^\d{12,18}$/"); using the test() method.

  • Do you need to check that the entered number is in a certain range? - Vladimir Martyanov
  • @ Vladimir Martianov honestly do not know, apparently yes. I was given the RegEx code and method. - Straight Edge

2 answers 2

Example. If the value is not suitable, the field is highlighted (pink background, for example). You may need to add a message to make it clear what is wrong. When you enter or copy in the field, the error highlighting is reset. Replaced the button type (it was button, put submit) to submit the form. Removed pieces of html markup so as not to clutter up.

 var regex = /^\d{12,18}$/; $('#payment-form').on('submit', function(e) { var ctNumber = $(this).find('input[name="Number"]'); var currentVal = $(ctNumber).val(); if (!regex.test(currentVal)) { $(ctNumber).addClass('invalid'); e.preventDefault(); } // тут добавить текст ошибки или вызвать другое событие или еще что-то e.preventDefault(); // это убрать }); $('#payment-form input[name="Number"]').on('input', function(e) { if ($(this).hasClass('invalid')) $(this).removeClass('invalid') }); 
 .invalid { background-color: #ffb9e1; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="payment-form"> <label>Номер</label> <input type="text" name="Number" placeholder="Номер" required /> <button type="submit">Принять</button> </form> 

    $ ("# bbankCard"). click (function test () {var whatinput;

    var number = new RegExp ("/ ^ \ d {12,18} $ /");

    whatinput = $ ("input.form-control"). val ();

    if () // here is a comparison with the test method whatinput and number; });

    jquerry Must be connected. either connect this script at the end or use ready ()

    • He added completely. (He suffered from regexp, because he refused to accept the expression in quotes) $ ("# bbankCard"). click (function test () {var whatinput; var number = new RegExp (/ ^ \ d {12,18} $ /); whatinput = $ ("input.form-control"). val (); alert (number); if (number.test (whatinput)) {alert ("its number");} else {alert (" error ");}}); substitute your handlers instead of alerts - Shezmu