RegularExpressionValidator to limit input length without character type restrictions

I am trying to use RegularExpressionValidatorto check input length TextBox. It works for me, but it only accepts letters and numbers. I would like to allow any characters, with the only check - no more than 25 characters.

<asp:TextBox ID="MenuLabel" runat="server" />
<asp:RegularExpressionValidator ValidationExpression="^[a-zA-Z0-9]{25}$" ID="MenuLabelVal" runat="server" ErrorMessage="Menu Label must be no longer than 25 characters." ControlToValidate="MenuLabel"  />

Regular expressions are not my strong suit, so can someone tell me how I change ^[a-zA-Z0-9]{25}$to represent any characters up to 25 times, and not just alphanumeric ones.

Note 1: I already have RequiredFieldValidatorone to provide 1 or more characters.

Note 2: I know that I can simply use the property MaxLengthin the TextBox, however this form is also intended for editing existing data, and I do not want it to simply trim existing records when editing. I would prefer to implement a validator that makes it obvious to users editing existing data, necessary to reduce the value, and not to truncate the form without implementation by the user.

Note 3: I am open to alternative solutions, such as a custom validator, if it relies only on client side validation. I do not have access to the code to write a custom validation handler on the server.

+5
source share
2 answers
.{1,25} 

char 1 25 . , unicode, char, ,

, ^.{1,25}$, ,

+14

, - :

<asp:CustomValidator ID="MenuLabelVal" runat="server" ClientValidationFunction="ValidateFieldLegth" ErrorMessage="Menu Label must be no longer than 25 characters." ControlToValidate="MenuLabel" EnableClientScript="true"  />   

javascript :

<script>     
    function ValidateFieldLegth(sender, args) {
        var v = document.getElementById('<%=MenuLabel.ClientID%>').value;
        if (v.length > 25) {
            args.IsValid = false;
        }
        else {
            args.IsValid = true;
        }
    }
</script>
+4

All Articles