开发者

Problem with Server Side Validation Control in .NET

I have an problem with Validation in .NET

I am using server side validation for a text box, i applied requiredfield and range validator for that. and my issue is i want to display messages in same place instead of using

for both the messages. suppose i left the textbox it says "it is required" and if i enter some value beyond the range it says "Range must be 1 to 100" but both the messages are in different places which doesn't seems good.

i don't want to use validation summary, i tried "validationGroup" and "Display" property, but i still didn't get the solution.

can anyone have any idea?

<asp:TextBox ID="txtEmpID" runat="server" CssClass ="textbox"></asp:TextBox>
            <br />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                ControlToValidate="txtEmpID" ErrorMessage="Emp ID is Must" 
                Display ="Dynamic"></asp:RequiredFieldValidator>
                <br />
            <开发者_Go百科asp:RangeValidator ID="RangeValidator1" runat="server" 
                ControlToValidate="txtEmpID" ErrorMessage="Range must be 1 -100" 
                MaximumValue="100" MinimumValue="1" Type="Integer" Display ="Dynamic"></asp:RangeValidator>


You can use CustomValidator and validate it manually. You will have the message in one place and one validation. The other solution is to have only RangeValidator and enable it even if the TextBox value is empty. ValidateOnEmpty="true" if i'm not wrong


You now have a hardcoded <br /> between the two validators. Remove that (and maybe add that to the ErrorMessage of the first validator: ErrorMessage="Emp ID is Must<br />").

You already have the Display=Dynamic so that a non-firing validator doesn't take up space.


ysrb is right, you only want the RangeValidator because the message "Range must be 1 -100" is applicable even if the text box is left empty

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜