ValidationGroup fired after OnClick - c#

I would like to do client-side validation to check if the textbox is empty before submit the form to server. However, the form is submit to server even though the textbox is empty.
<asp:Button ID="LinkButton2" runat="server" Text="Submit Order" CssClass="btn btn-lg btn-primary btn-block"
ValidationGroup="DeliveryAddVad" OnClick="SbmtOrder_Click" Width="150px" />
An error message will display under the textbox.
Is there anyway can stop the form submit to server if the textbox is empty.
The concern is OnClick="SbmtOrder_Click" will display a dialog, I do not want the dialog to show up if the textbox is empty.

You have to use a RequiredFieldValidator:
<asp:Button ID="LinkButton2" runat="server"
ValidationGroup="DeliveryAddVad"
UseSubmitBehavior="False"
Text="Submit Order"
CssClass="btn btn-lg btn-primary btn-block"
OnClick="SbmtOrder_Click"
Width="150px"
/>
<asp:RequiredFieldValidator runat="server" ID="reqOrder"
ValidationGroup="DeliveryAddVad"
controltovalidate="NameOfTextBox"
errormessage="Please enter [whatever the user has to enter]!"
/>
You can also try to set Button.UseSubmitBehavior to false (edited above) and the TextBox.CausesValidation to true(default false).

Related

Range Validator showing error message but not preventing submit button click

This is my html code. I wonder why this range validator is not preventing the submit button click. any help will really be appreciated.
<asp:TextBox runat="server" class="form-control"
ID="AGE" ReadOnly="false" required="true" />
<asp:RangeValidator ID="RVAGE" runat="server" MinimumValue="1"
MaximumValue="110" ErrorMessage="Range 1-110 "
ControlToValidate="AGE" Display="Dynamic">
</asp:RangeValidator>
<asp:Button OnClientClick="return CheckDouble();"
CausesValidation="true" OnClick="submitclick"
ID="submitbtn" runat="server" Text="Submit" >
</asp:Button>

Submit form after asp:RegularExpressionValidator check form valid?

I have a form with this markup:
<asp:TextBox ID="txtMeMail" runat="server" Width="250px" ToolTip="error"></asp:TextBox>
<asp:RegularExpressionValidator CssClass="mandatory msg" ID="RegularExpressionValidator1" runat="server" Display="Dynamic" ValidationGroup="validEmail" ValidationExpression="\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="txtMeMail" ErrorMessage="error" EnableClientScript="true" />
<button class="button noMarginLeft" runat="server" validationgroup="validEmail" accesskey="S" id="btnSave" onserverclick="BtnSave_Click" value="Page.FIUserEdit.SaveButton.Label">
<asp:Label runat="server" Text="label"></asp:Label>
</button>
When I input "abc" and remove the focus from the text box, the invalid email message shows up. After that, I correct the text box with a valid email and I keep the focus on the text box, then I click the submit button. The validation message disappears but the form does not submit.
Is there any way to validate and then submit the form?
Try using
<asp:Button class="button noMarginLeft" runat="server" ValidationGroup="validEmail" accesskey="S" ID="btnSave" OnServerClick="BtnSave_Click" value="Page.FIUserEdit.SaveButton.Label" />
The problem is that you use the ordinary html Button tag. Although it can work it is not recommended because you lose functionality, see How can I use the button tag with ASP.NET?
If you do want content inside the button then use the LinkButton.

prevent The Client side validation go to server side

I have a form and I have a validation in JavaScript. How to prevent the submit button go the the server side if the form is not valid?
<button id="LoginButton" onclick="Login.initReuiredValidation();"
onserverclick="LoginButton_Click" runat="server" type="submit"
class="submit btn btn-primary pull-right">
<asp:Literal runat="server" meta:resourcekey="LoginButton" />
<i class="icon-angle-right"></i>
</button>
in jquery you can do as
$(yourform).submit(function(event){
event.preventDefault();
//do somehting
})
This isn't classic asp, it's asp.net webforms and I've edited the tags accordingly
Adding input validation in webforms is quite easy, you can assign a validation control to each form control, eg
Contact Name<br />
<asp:TextBox ID="Contact_name" runat="server" Width="246 pt"></asp:TextBox>
<asp:RequiredFieldValidator ID="ContactNameValidator" runat="server"
ErrorMessage="Please enter your name" ControlToValidate="Contact_name"></asp:RequiredFieldValidator><br />
<br />
Contact Telephone<br />
<asp:TextBox ID="Contact_phone" runat="server" Width="246pt"></asp:TextBox>
<asp:RequiredFieldValidator ID="ContactPhoneValidator" runat="server" ControlToValidate="Contact_phone"
ErrorMessage="Please enter your telephone number"></asp:RequiredFieldValidator>
If you view the output, you will see that the validation is actually done with (client side) JavaScript. Asp.net generates that for you, you don't have to write it yourself.
Further information here
https://msdn.microsoft.com/library/a0z2h4sw%28v=vs.100%29.aspx
In the code behind add...
LoginButton.Attributes.Add("onclick", "return false;");

How to use server side validation on button click using Asp.net

I have 2 buttons on my page and i want to validate only on btnSubmit click. However i don't want to validate if user clicks the btnSave button. I have several text-boxes that i need to validate..
<asp:TextBox ID="txt1" runat="server" CssClass="form-control"></asp:TextBox>
<asp:RequiredFieldValidator ErrorMessage="** Required **" ForeColor="Red" ControlToValidate="txt1" runat="server" Display="Dynamic" />
here is the button save
<asp:LinkButton ID="linkSave" runat="server" CssClass="btn btn-primary btn-block" OnClick="btn_Save_Click">
<i aria-hidden="true" class="glyphicon glyphicon-ok"></i>Save
here is the button submit
<asp:LinkButton ID="linkSubmit" runat="server" CssClass="btn btn-primary btn-block" OnClick="btn_Submit_Click">
<i aria-hidden="true" class="glyphicon glyphicon-ok"></i>Submit
Set CauseValidation = "False" on all the buttons you don't want to trigger validation, including LinkButtons
<asp:LinkButton ID="linkSave" runat="server" CauseValidation="False" />
If you want different validations per button, then use the ValidationGroup="GroupName" attribute on the button and all the controls participating in that validation (associated to that button)
Use validation groups.
This allows you to make one button enforce validation (btnSubmit) but the other not (btnSave).
you can use group validation
<asp:TextBox ID="txt1" runat="server" CssClass="form-control"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" validationgroup="validTxT1Group" ErrorMessage="** Required **" ForeColor="Red" ControlToValidate="txt1" runat="server" Display="Dynamic" />
<asp:LinkButton ID="linkSave" runat="server" CssClass="btn btn-primary btn-block" CausesValidation="false" OnClick="btn_Save_Click">
<i aria-hidden="true" class="glyphicon glyphicon-ok"></i>Save
<asp:LinkButton ID="linkSubmit" runat="server" CssClass="btn btn-primary btn-block" CausesValidation="true" ValidationGroup="validTxT1Group" OnClick="btn_Submit_Click">
<i aria-hidden="true" class="glyphicon glyphicon-ok"></i>Submit

asp:Button and asp:LinkButton and asp:Button submitting on enter in Chrome

In Internet Explorer if I hit enter in the TextBox P it submits the Form using the onclick event of LoginButton.
This is what I want to happen.
In Google Chrome if I hit enter it submits the form with the onclick of Button1.
This is not what I want to happen.
Button1 is actually not visible on the form under normal circumstances. It is made visible under certain circumstances to do a different task then login.
How can I force this, browser independently, to always use LoginButton onclick when someone presses enter?
<asp:TextBox ID="P" runat="server" TextMode="Password" Width="150"></asp:TextBox>
<asp:LinkButton CssClass="button" ID="LoginButton"
runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1"
onclick="LoginButton_Click" />
<asp:Button
ID="Button1" runat="server"
Text="Submit" onclick="Button1_Click" />
You set the forms default button:
<form id="Form1" defaultbutton="SubmitButton" runat="server">
The Following works for me.
<form id="form1" runat="server" defaultbutton="LoginButton">
<div>
<asp:TextBox ID="P" runat="server" TextMode="Password" Width="150"></asp:TextBox>
<asp:LinkButton CssClass="button" ID="LoginButton"
runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1"
OnClick="LoginButton_Click" />
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" > </asp:button>
</div>
</form>
You'll probably want to set a default button in your Page_Load method, like this:
this.Form.DefaultButton = this.LoginButton.UniqueID;
I ended up using C# in my Page_Load(object sender, EventArgs e) method to set the DefaultButton. Found this on another stackoverflow post.
https://stackoverflow.com/a/2213237/2907463
this.Form.DefaultButton = Login1.FindControl("LoginButton").UniqueID;

Categories