开发者

Validation using JavaScript

The below code to validate TextBox is working fine onkeypress but when I use backspace to reduce the text length this code is unable to return back to the RED color. How to change color back to red on using backspace to reduce string length.

<script type="text/javascript">

    function limitlength(obj, length) {
        var maxlength = length
        if (obj.value.length > maxlength) {
            document.getElementById("TextBox1").style.background开发者_StackOverflowColor = "green";
        }
        else
        {
            document.getElementById("TextBox1").style.backgroundColor = "red";
        }

    }

</script>

Enter text (max length is 5 characters):
<form id="form1" runat="server">
<br />
<br />
<asp:TextBox ID="TextBox1" onkeypress="return limitlength(this, 5)" runat="server"></asp:TextBox>
<br />
<br />
</form>


try using onkeydown or onkeyup event.


Either use onchange or onkeyup. I think it's better if you use onkeyup

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜