简体   繁体   中英

How to validate expression on button click in asp.net C#

I am making a website and I have made a form fields like email field and validation expressions associated with it. Validation is initiated on text change. But i want it t execute on "submit" button click event. I have searched but could not locate the solution to my problem. Please tel me why is this happening and how can i make it right. I am new to this web development field, So need help from you guys.

Thanks in advance!!!

Hamad

You could disable showing errors in the validator itself and instead make a validation summary which will be shown only after you click submit.

Like this:

<asp:RequiredFieldValidator runat="server" ControlToValidate="txtEmail" ValidationGroup="vRegister" Display="None" ErrorMessage="Email field cannot be empty"></asp:RequiredFieldValidator>

and then declare a validation summary:

<asp:ValidationSummary runat="server" ID="vSummary" ValidationGroup="vRegister" DisplayMode="BulletList" />

What you should do is change the value of EnableClientScript to false. Then call the validation from your code behind (which should always be done since a user can disable their client side validation anyway. Security rule 1, never trust the client)

EnableClientScript: Gets or sets a value indicating whether client-side validation is enabled.

 <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
 <asp:RequiredFieldValidator ID="rfvName" runat="server" EnableClientScript="false"
 ErrorMessage="*" ControlToValidate="txtName" ></asp:RequiredFieldValidator>

Code Behind:

   protected void btnSave_Click(object sender, EventArgs e)
   {
          if (Page.IsValid)
             {
               //Do stuff
             }
             //No need for else, the validations should display accordingly
   }

Additional resources: http://weblogs.asp.net/rajbk/archive/2007/03/15/page-isvalid-and-validate.aspx

Are you simply looking for the validation type controls to validate input? If so look at the property EnableValidation and set it to true. Doing so will force the validation of the textbox even before the button_Click event can execute.

Try this:

 <asp:TextBox ID="txtName" runat="server" Height="23px" 
 Width="252px"></asp:TextBox>
 <asp:RequiredFieldValidator ID="rfvName" runat="server" 
 ErrorMessage="*" ControlToValidate="txtName" 
 ValidationGroup="vadd"></asp:RequiredFieldValidator>

If i have understood your question, you need to create a same validation group for each of the validation control in your aspx page. You also need to have a validation summary with same validation group. And atlast in the submit button in aspx page you have to mention same validation group...

The "Causes Validation" property on the button itself will automatically force your page to meet you validation specifications before firing the rest of the code associated with the button press.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM