Hamad
Hamad

Reputation: 39

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

Upvotes: 3

Views: 18173

Answers (6)

Pratik.S
Pratik.S

Reputation: 470

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...

Upvotes: 0

KreepN
KreepN

Reputation: 8598

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.

Upvotes: 0

Ed Charbeneau
Ed Charbeneau

Reputation: 4634

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

Upvotes: 5

BlackKnights
BlackKnights

Reputation: 135

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>

Upvotes: 1

Stanislav Ageev
Stanislav Ageev

Reputation: 778

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" />

Upvotes: 6

JonH
JonH

Reputation: 33153

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.

Upvotes: 1

Related Questions