简体   繁体   English

在javascript中调用onkeyup事件的函数进行输入屏蔽

[英]calling a function for onkeyup event in javascript for input masking

I have a html code which will do the input masking for the date entry which is given below 我有一个html代码,它将为下面给出的日期输入做输入屏蔽

<html>

<input
    type="text"
    name="date"
    placeholder="dd/mm/yyyy"
    onkeyup="
        var v = this.value;
        if (v.match(/^\d{2}$/) !== null) {
            this.value = v + '/';
        } 
        else if (v.match(/^\d{2}\/\d{2}$/) !== null) {
            this.value = v + '/';

        }

     "
    maxlength="10"
>
</html>


It takes the input in the specified format and this is working fine,, but I wanted to call this as a function and I tried this 它以指定的格式接受输入,并且工作正常,但是我想将此作为函数调用,并且尝试了

<br><script type="text/javascript">

function check(t){
        var v = t;
        if (v.match(/^\d{2}$/) !== null) {
            t = v + '/';
        return;
        } 
        else if (v.match(/^\d{2}\/\d{2}$/) !== null) {
            t = v + '/';
        return;

    }
}

</script>
<html>


<input
    type="text"
    name="date" style="text-transform: uppercase";
    placeholder="dd/mm/yyyy"
    onkeyup="check(this.value);"
    maxlength="8"
>
</html>

but i didn't get the required result. 但我没有得到所需的结果。 it is taking the value as ,say 11111111 where it should have taken 11/11/1111 它采用的值是,例如11111111,应该采用11/11/1111的值
What may be wrong here? 这有什么问题吗?

You have to give like this , If you want to reflect the changes into textbox 如果要将更改反映到文本框中,则必须这样给

function check(t){
          var v = t;

        if (v.match(/^\d{2}$/) !== null) {

            t = v + '/';
            document.getElementById("t1").value=t;
        return ;
        } 
        else if (v.match(/^\d{2}\/\d{2}$/) !== null) {
            t = v + '/';
            document.getElementById("t1").value=t;
        return ;

    }
}

working fiddle 工作小提琴

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

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