prevent The Client side validation go to server side - c#

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;");

Related

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.

ASP.NET Webforms - ValidationSummary show distinct errors

When I am using ValidationSummary to validate my page if I have duplicate errors my validation will show all this errors. I want to display a distinct list of errors. I think that the best approach is to ovverride an event. But I don't know what event to override. What is the event that deals with showing errors.
I don't want solutions for MVC projects!
ValidationSummary collect all the error in your input and display.
So indirectly you have answerd your question by yourself in your question
You just don't know the syntax i think.Here it is:
If you have some collection of input in aspx,you have defined also the regular expression for specific input.For example:
<div>
<asp:TextBox ID="txt" runat="server" MaxLength="100"></asp:TextBox>
<asp:RegularExpressionValidator ID="revtxt" runat="server"SetFocusOnError="true"ErrorMessage="Please enter correct txt" ControlToValidate="txt" ValidationGroup="Submit"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
<div>
<div>
<asp:TextBox ID="txt1" runat="server" MaxLength="100"></asp:TextBox>
<asp:RegularExpressionValidator ID="revtxt1" runat="server"SetFocusOnError="true"ErrorMessage="Please enter correct txt1" ControlToValidate="txtEmail" ValidationGroup="Submit"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
<div>
<div>
<asp:Button ID="btnSubmit" runat="server" Text="Save" ValidationGroup="Submit" OnClick="btnSubmit_Click" />
</div>
<div>
<asp:ValidationSummary ID="ValidationSummary"runat="server"ValidationGroup="Submit" />
</div>
So in every input or button you have to define ValidationGroup attribute
Else if you want to check in codebehind if all of this input are validated you have to do this:
if(Page.IsValid)
{
//your code here
}

ValidationGroup fired after OnClick

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

Using button as link that executes javascript prevents OnClick from working

I am using a button as a link because when the button is pressed it opens a popup of In Line HTML using Javascript (http://www.enthropia.com/labs/ibox/):
<asp:Button ID="searchButton" runat="server" Text="Search Stories" OnClick="searchButton_Click" />
However, this prevents the OnClick C# from executing when I press the button. How do I fix this?
I suggest you this code
<a href="#inner_content" rel="ibox&width=800&height=400" title="Search Non Scrum Stories">
</a>
<asp:Button ID="searchButton" runat="server" Text="Search Stories" OnClick="searchButton_Click" />
David you must separate html link and button (it's not xaml)
Alternatively, you can use LinkButton - http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.aspx
Example (from the linked documentation):
<asp:LinkButton id="LinkButton1"
Text="Click Me"
Font-Names="Verdana"
Font-Size="14pt"
OnClick="LinkButton_Click"
runat="server"/>
separate the tags and put your button in as a submit button or you can do something like:
<form action="ur_popup_page" onsumit="javascriptsunction()">
<asp:Button type="submit" id="searchbutton"/>
</form>

Clear button should not required field HTML5

im creating the form include below code :
<asp:TextBox ID="txtPhone" required="required" runat="server" placeholder="Phone No. (eg. 999)"
CssClass="glowStyle"></asp:TextBox>
<br />
<asp:Button ID="btnAddDriver" Text="Add" runat="server" CssClass="button" />
<br />
<asp:Button ID="btnCnclAddDriver" Text="Clear" runat="server"
CssClass="cancelbutton" onclick="btnCnclAddDriver_Click" />
</form>
when i click the "Clear" button, the required (html5) still show up.
how to disable it for Clear button only.
Add formnovalidate attribute to your cancel/clear button.
Here you can find some more information about this attribute.
Do you actually require a server control to cancel ? A simple <input type="reset"> or a small javascript code can do the job in most situations.
If not, simple add CauseValidation="false" to your button :
<asp:Button ID="btnCnclAddDriver" CauseValidation="false" Text="Clear" runat="server"
CssClass="cancelbutton" onclick="btnCnclAddDriver_Click" />
(remove suggestion as it applies to asp.net validation)
You can set the autopostback option for btnCnclAddDriver button to False. And execute your server side code (btnCnclAddDriver_Click) by Ajax.
You can clear your TextBox(s) with javascript (jQuery) :
$('txtPhone').val('');
Or Javascript :
function name()
{
document.getElementById('txtPhone').value = "";
}
Hope this help.

Categories