简体   繁体   English

使用空格验证用户输入

[英]Validate user input with whitespace

How to in real-time format user input?如何实时格式化用户输入? For example, the user puts A9364470240ZGS001 to the input field, and using JavaScript format it in the input field in real-time to be like: A 936 447 02 40 ZGS 001 ?例如,用户将A9364470240ZGS001放入输入字段,并在输入字段中实时使用 JavaScript 格式为: A 936 447 02 40 ZGS 001

<div class="childDumpFile">
     <label for="ds">Dataset</label>
     <input type="text" class="form-control" id="ds" name="ds" value="{{Request::get('ds') ?? ''}}">
</div>

If we suppose users have to write the characters one by one.如果我们假设用户必须一个一个地写字符。 This will work.这将起作用。

<body>
    <input type="text" class="form-control" id="ds" name="ds" onkeypress="keyPress()" maxlength="23">
</body>
<script>
    function keyPress() {
        var field = document.getElementById("ds");
        var text = field.value;
        if(text.length == 1 || text.length == 5 
        || text.length == 9 || text.length == 12
        || text.length == 15 || text.length == 19 ) {
            var newText = text + " ";
            field.value = newText;
        }
    }
</script>

I found the true answer.我找到了真正的答案。 These expectations are naming as "input-mask" and if you'd like to use.如果您愿意,这些期望被命名为“输入掩码”。 You have to use 3. party libraries.您必须使用 3. 方库。 Some of them listing in following sites:其中一些在以下网站中列出:

Libraries 1 Libraries 2图书馆 1图书馆 2

I chose Cleave.js for your question.我为您的问题选择了Cleave.js This is the demo:这是演示:

<script src="https://nosir.github.io/cleave.js/dist/cleave.min.js"></script>
<script src="https://nosir.github.io/cleave.js/dist/cleave-phone.i18n.js"></script>
<script>
    function loadFunction() {
        // custom
        var cleaveCustom = new Cleave('.input-custom', {
            blocks: [1, 3, 3, 2, 2, 3, 3],
            delimiter: ' ',
        });
    }
</script>

<body onload="loadFunction()">
    A 936 447 02 40 ZGS 001
    <div class="container">
        <input class="input-custom" placeholder="Custom delimiter & blocks" />
    </div>
</body>

Here is a little example.这是一个小例子。

<div class="childDumpFile">
     <label for="ds">Dataset</label>
     <input type="text" class="form-control" id="ds" name="ds">
</div>

<div class="test_ds"></div>

JS with jquery. JS 与 jquery。

$("#ds").change(function(){
    var ds_value = $("#ds").val();

var temp = ds_value;
temp = temp.substring(0,1) + " " + temp.substring(1, 4) + " " + temp.substring(4, 7) + " " + temp.substring(7, 9) + " " + temp.substring(9, 11) + " " + temp.substring(11, 14) + " " + temp.substring(14, 17);

  $("#ds").val(temp);
  $(".test_ds").html(temp);
});

Here is a demo - https://jsfiddle.net/Kistlak/7bkdtev8这是一个演示 - https://jsfiddle.net/Kistlak/7bkdtev8

First, you need add onchange="getTimeNow()" oninput="getTimeNow()" in to input首先,您需要在输入中添加 onchange="getTimeNow()" oninput="getTimeNow()"

<input type="text" class="form-control" id="ds" name="ds" value="{{Request::get('ds') ?? ''}}" onchange="getTimeNow()" oninput="getTimeNow()">

Finally, you get event input text最后,您将获得事件输入文本

<script>function getTimeNow(){console.log(new Date())}</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM