1
<asp:TextBox ID="Textboxtotalamount" OnTextChanged="AmountChanged"
         Width="90px" AutoPostBack="true" runat="server">
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator" runat="server" 
         ControlToValidate="Textboxtotalamount"
         SetFocusOnError="True" ValidationGroup="val">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
         ControlToValidate="Textboxtotalamount"
         ValidationExpression="^[-+]?[1-9]\d{0,13}(\.\d{1,3})?%?$"
         SetFocusOnError="True">F</asp:RegularExpressionValidator>

in this above code can validate correctly.......but in text box i call "OnTextChanged" event ....suppose if i type(characters)in textbox errormesge shown at the same time ontextchanged event also called hen error occured ....i ve to stop OnTextChanged event when regularexpression raised...... pls help

Heinzi
  • 167,459
  • 57
  • 363
  • 519
Domnic
  • 3,817
  • 9
  • 40
  • 60
  • Domnic, Once you have regular expression validator and required field validator, they validate the user input via JavaScript at the client end only, now in case they are passed then only AmountChanged event will be fired at server end (in case you want Amount Changed at client end then you need to add page_ClientValidate() in Javascript). – Ashish Jain Nov 24 '09 at 11:25
  • @Beginner: I beg to differ: RegularExpressionValidator performs client-side *and* server-side validation. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.regularexpressionvalidator.aspx – Heinzi Nov 24 '09 at 11:32
  • @Beginner - both RequiredFieldValidator and RegularExpressionValidator perform client and server side validation. – Russ Cam Nov 24 '09 at 12:49
  • BTW, this question is an exact duplicate of http://stackoverflow.com/questions/1789355 – Heinzi Nov 24 '09 at 13:25

2 Answers2

2

I'm not sure I understood your question correctly: You want to execute the `AmountChanged´ code only if the validator did not report any errors, right? In that case, you should call

if (!this.IsValid)
    return;

at the start of AmountChanged. (You might need to call this.Validate(); first, if OnTextChanged does not initiate validation by itself.)

Details can be found here: http://msdn.microsoft.com/en-us/library/dh9ad08f(VS.100).aspx


Clarification: At the moment, your code looks like this:

void AmountChanged(...) {
    ...
}

You need to change it to this:

void AmountChanged(...) {
    if (!this.IsValid)
        return;

     ...
}

so that the code is not executed when some validator detects an error. In addition, you must add CausesValidation="true" to your TextBox as Saar's example shows.

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
2

I updated your code. see if this helps you.

<asp:TextBox ID="Textboxtotalamount" OnTextChanged="AmountChanged" ValidationGroup="val"
         Width="90px" AutoPostBack="true" runat="server" CausesValidation="true">
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator" runat="server" ValidationGroup="val"
         ControlToValidate="Textboxtotalamount"
         SetFocusOnError="True" >*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
         ControlToValidate="Textboxtotalamount" ValidationGroup="val"
         ValidationExpression="^[-+]?[1-9]\d{0,13}(\.\d{1,3})?%?$"
         SetFocusOnError="True">F</asp:RegularExpressionValidator>
Saar
  • 8,286
  • 5
  • 30
  • 32
  • 1
    @Heinzi: yes. But it at client side. Still one should have "this.Validate" to be validated at server side too. good practice ;) – Saar Nov 24 '09 at 12:01
  • ya one issue if i write characters message shown when i press update button then amountchanged() event called and error occured how can i handle in amountchangedevent() – Domnic Nov 24 '09 at 12:08
  • 1
    @Saar: So CausesValidation does not initiate server-side validation? – Heinzi Nov 24 '09 at 12:15
  • i removed validation group="val" in textbox event then worked partialy but when i update the error occured so in amountchaned() event how should i handle – Domnic Nov 24 '09 at 12:18
  • 1
    @Heinzi: Good question. http://msdn.microsoft.com/en-us/library/system.windows.forms.control.causesvalidation.aspx I am not asp.net expert, but as explained at msdn, it should be at client side. Need to investigate/study further. – Saar Nov 24 '09 at 13:10
  • 2
    @Domnic: Check following http://stackoverflow.com/questions/1039465/should-i-always-call-page-isvalid-in-asp-net-webforms-c – Saar Nov 24 '09 at 13:11