c# - Sequencing ASP.NET custom validators and difference between Validator's Text and ErrorMessage properties -
i have textbox
2 validators. first validator checks if textbox
empty or not. second validator checks value of textbox
contains spaces or not. when run project , try validate without text in textbox
, shows both validators' error messages. want should not execute second validator until first validator validated successfully.
<asp:customvalidator id="customvalidator1" runat="server" controltovalidate="textbox3" errormessage="please enter value." font-names="segoe ui" onservervalidate="customvalidator1_servervalidate" setfocusonerror="true"></asp:customvalidator> <br /> <asp:customvalidator id="customvalidator2" runat="server" controltovalidate="textbox3" errormessage="spaces not allowed." font-names="segoe ui" onservervalidate="customvalidator2_servervalidate" setfocusonerror="true"></asp:customvalidator> <br />
so questions are:
how can sequence validations 1 validation should called after other validated successfully?
another question want ask difference between validator's text , errormessage properties?
you should use requiredfieldvalidator
empty text, , customvalidator
check out string composition.
<asp:requiredfieldvalidator id="requiredfieldvalidator1" controltovalidate="textbox3" runat="server" errormessage="please enter value."> </asp:requiredfieldvalidator> <br /> <asp:customvalidator id="customvalidator2" runat="server" controltovalidate="textbox3" errormessage="spaces not allowed." font-names="segoe ui" onservervalidate="customvalidator2_servervalidate" setfocusonerror="true"> </asp:customvalidator> <br />
the errormessage msdn:
gets or sets text error message displayed in validationsummary control when validation fails.
the text msdn:
gets or sets text displayed in validation control when validation fails. (overrides label.text.)
edit:
given doing multiple validations, should use single customvalidator
this. in server side, should check both, empty , string composition, this:
protected void customvalidator1_servervalidate(object source, servervalidateeventargs args) { if (string.isnullorempty(args.value)) { args.isvalid = false; ((customvalidator)source).text = "please enter value."; } else if (/*check if has empty space*/) { args.isvalid = false; ((customvalidator)source).text = "spaces not allowed."; } else { args.isvalid = true; } }
Comments
Post a Comment