开发者

How to add a field validator and set it programmatically

开发者 https://www.devze.com 2023-04-11 01:04 出处:网络
I need to add a field validator, the admin of the website sets the number of characters of a textbox in admin and then... in the front side users can only type upto the number of characters specified

I need to add a field validator, the admin of the website sets the number of characters of a textbox in admin and then... in the front side users can only type upto the number of characters specified by the admin.

In the fron end i aleady pull the number from the database but now i am not sure how to create a validator where i can set the value say:

pseudocode

Get the validator
 set the validator to validat开发者_运维知识库e these maximum number of characters
     myValidator.setamaxnumber = mydbvalue;
     error message = Only + mydbvalue + charaters are accepted.

I hope you understand what i want to do. If not, no need to do a negative vote post your question and i will try my best to supply more information

Thanks a million


Try using the RegularExpressionValidator control like so:

Markup

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="MaxLengthValidator" runat="server" ControlToValidate="TextBox1"></asp:RegularExpressionValidator>

Codebehind

protected void Page_Load(object sender, EventArgs e)
{
    var maxLength = 10;
    MaxLengthValidator.ValidationExpression = @"^[\s\S]{0," + maxLength.ToString() + "}$";
    MaxLengthValidator.ErrorMessage = string.Format("Only {0} charaters are accepted.", maxLength);
}
0

精彩评论

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