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
}
Related
I have a page that has two separate search forms, one for searching by item ID and one for searching by keywords. It looks like this:
I started with just the top one, I am now adding the bottom one. The bottom one is basically a copy and paste of the first one (both are components in Sitecore). This is the code for each:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="SearchByItemNumber.ascx.cs" Inherits="Bayer.CropScience.SC.Internet.US.Website.layouts.Bayer_CropScience.Scope.Country_United_States_Internet.RewardsCatalog.SearchByItemNumber" %>
<div class="form search-item">
<div class="clearfix">
<h3>
<label class="searchByItemNumber" for="item-num">Know your item number?</label></h3>
<div class="input-wrapper search-by-itemnumber-length">
<asp:TextBox runat="server" ID="txtSearchItemNumber" CssClass="search-by-itemnumber-length" placeholder="Search by Item Number" />
<asp:RequiredFieldValidator runat="server" ID="rfvSearchItemNumber" ControlToValidate="txtSearchItemNumber" ValidationGroup="searchCatalog" ErrorMessage="Please fill out this field." CssClass="error"></asp:RequiredFieldValidator>
</div>
<asp:Button runat="server" Text="Go" CssClass="small" OnClick="Search" ValidationGroup="searchCatalog" />
</div>
</div>
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="SearchByDescription.ascx.cs" Inherits="Bayer.CropScience.SC.Internet.US.Website.layouts.Bayer_CropScience.Scope.Country_United_States_Internet.RewardsCatalog.SearchByDescription" %>
<div class="form search-item">
<div class="clearfix">
<h3>
<label class="searchByKeywords" for="item-keywords">Search by keywords</label></h3>
<div class="input-wrapper search-by-keywords">
<asp:TextBox runat="server" ID="txtSearchKeyword" CssClass="search-by-keywords" placeholder="Search by keywords" />
<asp:RequiredFieldValidator runat="server" ID="rfvSearchKeyword" ControlToValidate="txtSearchKeyword" ValidationGroup="searchCatalogKeywords" ErrorMessage="Please fill out this field." CssClass="error"></asp:RequiredFieldValidator>
</div>
<asp:Button runat="server" Text="Go" CssClass="small" OnClick="KeywordSearch" ValidationGroup="searchCatalogKeywords" />
</div>
</div>
The problem is that when I fill out an item number and click Go, I get the validation error message "Please fill out this field" under the keywords box, and vice versa. How do I stop this from happening?
EDIT - The validation error occurs when I press "enter" but NOT when I click the "Go" button
EDIT 2 - It was a javascript issue, pressing enter was causing both fields to validate. I overrode it with a custom onkeypress function
You need to have different ValidationGroups. You're using ValidationGroup="searchCatalog" on all fields.
Assign a Unique ValidationGroup to fields that you want to be validated together.
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.
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;");
I'm using a FormView control to allow users to insert rows to the database. I want to validate these input fields, and as such have added a regular expression validation helper. Here's the markup:
<InsertItemTemplate>
<p>
Name:
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
<asp:RegularExpressionValidator ValidationExpression="^[a-zA-Z0-9 ]*$" ControlToValidate="NameTextBox" ID="NameTextBoxValidator" runat="server" ErrorMessage="Must be alphanumeric characters and spaces"></asp:RegularExpressionValidator>
</p>
<p>
Location:
<asp:TextBox ID="LocationTextBox" runat="server"
Text='<%# Bind("Location") %>' />
</p>
<p>
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
</p>
</InsertItemTemplate>
However, when I click InsertButton the page refreshes and I get an error from SQL Server saying it can't insert a NULL value, the validator isn't getting used at all.
How can I fix this?
I assume that the user entered no text and the database does not allow null values.
A RegularExpressionValidator will not validate empty controls. So you need to provide also a RequiredFieldValidator.
The validation will not fail if the input control is empty. Use the
RequiredFieldValidator control to make the field required.
http://www.w3schools.com/aspnet/control_regularexpvalidator.asp
Not much info here, but I'll venture a guess:
Check to make sure you don't have anything happening OnLoad that's blanking things out. If you do have an OnLoad make sure it only fires when IsPostback is false.
I'm having an issue getting a regex field validator to work for an asp page i'm trying to update.
Here is the asp:Panel stripped down to the important bits:
<asp:Panel ID="pnlEmailAddressCollection" runat="server">
<div id="POMInput-wrapper">
<div class="POMInput-FieldText">
<span class="POMInput-wrapper-text">Name:</span>
<br />
<span class="POMInput-wrapper-text">Email Address:</span>
<br />
</div>
<div class="POMInput-FieldEntry">
<asp:TextBox ID="txtEmailAddress" name="emailAddress" runat="server" CssClass="textInput"></asp:TextBox>
<asp:TextBox ID="txtUserName" runat="server" name="firstName" CssClass="textInput"></asp:TextBox>
</div>
<asp:RequiredFieldValidator ID="rfvNameValidator" runat="server"
ErrorMessage="Please enter your name"
ControlToValidate="txtUserName"
Display="None" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please enter your email address"
ControlToValidate="txtEmailAddress"
Display="None" />
<asp:RegularExpressionValidator ID="rfvEmailValidator2" runat="server"
ErrorMessage="Please enter a valid email address"
ControlToValidate="txtEmailAddress"
Display="None"
ValidationExpression="^[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$" />
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
ShowMessageBox="true"
ShowSummary="false"
EnableClientScript="true" />
</div>
</asp:Panel>
It is currently failing on any email i put in. The asp:RequiredFieldValidator's work as expected.
I tested the regular expression in a test project and the regex seems good (returns true on valid emails, false on invalid ones). Did I set up the asp:RegularExpressionValidator incorrectly?
You should remove double backslash:
ValidationExpression="^[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$"
Note that you put two backslashes where you meant only one. If you were to set this expression from code behind, the string you provided is correct. But in aspx you don't have to escape backslash.
At the moment accepted email address would be something like abc#abc{backslash}.com
You can try with this code
ValidationExpression="[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
Nota : you can delete ^ and $ symbols
The regular expression works within .NET (server-side), but is failing due to the client-side JScript implemetation as documented in the Remarks section. To verify this (it passes server-side validation), set the EnableClientScript property on the validator to false.
Then undo that change and verify the regex will pass on the client side. You can use an online tester, if it's easier for you.