It is necessary that when entering into an input, only one space is allowed. I suppose that this requires a regular expression. I just can't find it ... Maybe someone has other options. ) help me please
Closed due to the fact that the essence of the issue is not clear to the participants of the Air , 0xdb , Dmitry Kozlov , Jarvis_J , user192664 Nov 9 '18 at 4:06 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
|
2 answers
$("#enter").click(function() { var text = $('#input').val();//Получаем значения из инпут var count = text.match(/[ ]/g);//Ищим пробелы if(count) {//Если есть пробелы count = count.length;//Считаем кол-во пробелов if(count >=1) {//Проверяем alert('Введено максимальное кол-во пробелов'); } } }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="input"> <button id="enter">Enter</button> - 2Vladimir needs only one space to be allowed when entering the input. Correct the condition in the code - Aleksey Muratov
- Thank you very much! - Vladimir Lipodat 2:01
|
And here I am another such option realized
$(document).on('keydown', '.input-hide', function(e) { if ($(this).val().indexOf(' ') !== -1 && e.keyCode === 32) return false; }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class='input-hide' placeholder="ограничение пробелов"> |