Cергій Крепкій Cергій Крепкій - 7 months ago 12
Javascript Question

how to use getElementById to check length

What I'm doing wrong?and maybe somebody tells me how to check length not bigger numbers not from keybord but buttons?

function checkLength()
{
var input = document.getElementById("t") ;
{
if(input.value.length == 10)
{
input = input.replace(input, '');
alert("Only 10 numers") ;
}
}
}


<input type="text" maxlength="10" name="answer" id="t" onkeyup="isAllowedSymbol(this);checkLength();" placeholder="Enter data" >

Answer

You have "{}" left in your code, this code will work well:

function checkLength()
    {
        var input = document.getElementById("t") ;
            if(input.value.length >= 10)
            {
                var value = input.value;
                input.value = value.slice(0,10);
                alert("Only 10 numers") ;

            }

    }