admin管理员组

文章数量:1415074

I have a html code which will do the input masking for the date entry which is given below

<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
What may be wrong here?

I have a html code which will do the input masking for the date entry which is given below

<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
What may be wrong here?

Share Improve this question edited Nov 20, 2013 at 14:51 Messi L asked Nov 20, 2013 at 14:45 Messi LMessi L 972 silver badges11 bronze badges 2
  • do you get a js error ? – geevee Commented Nov 20, 2013 at 14:48
  • no errors, it is taking the value as say 11111111 where it should have taken 11/11/1111 – Messi L Commented Nov 20, 2013 at 14:50
Add a ment  | 

1 Answer 1

Reset to default 3

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

本文标签: htmlcalling a function for onkeyup event in javascript for input maskingStack Overflow