开发者

Limit the Textbox to a range of characters in VB

Hey guys, I want to limit the Textbox to a range of characters in VB. For example, I want the user to enter strings from 5-7 characters. I know how to limit the Textbox to a certain number of characters by MaxLength bu开发者_如何学Ct that will give him the option of writing just 3 or 4 characters which I don't want to allow that from happening.

Any tips?


Handle the Control.LostFocus event and check for the minimum length.

Private Sub TextBox1_LostFocus(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles TextBox1.LostFocus
     if TextBox1.Text.Length < minLength Then
        TextBox1.Focus()
     End If
End Sub


You will have to handle textbox.keyup. and run a regex to see if that key entered is not one of the characters you don't want


I did this in other way which I handled it by creating (if statement) with a condition textLength < 5 Then a messageBox appear with an error. That solved my problem as well =)


What about JS? Take a look buddy;

<script type="text/javascript">
    function minMaxLenght(textbox) {
        if (textbox.value.length < 3 || textbox.value.length > 5)
            alert('Invalid, do some work!');
    }
</script>



<asp:TextBox runat="server" ID="TextBox1" onblur="javascript: minMaxLenght(this);" 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜