I have a custom control included in a form that includes a dropdown list. The form has a number of other required fields, so i was wondering how to validate this dropdown.
<gaia:TextBox ID="TitleTextBox" runat="server"/>
<gaia:RequiredFieldValidator runat="server" ControlToValidate="TitleTextBox"
ErrorMessage="Please fill in the press release title" Text="*" Display="None" ValidationGroup="save" />
<CN:ProductCategoryDropDown runat="server" ID="ProductCategoryDropDown" />
<gaia:CustomValidator runat="server" ID="ProductCategoryValidator" OnServerValidate="ProductCategory_Validate" ValidationGroup="save"
Display="None" Text="*" ErrorMessage="Please select a category" />
the code behind looks like this
protected void ProductCategory_Validate(object source, ServerValidateEventArgs args)
{
args.IsValid = (ProductCategoryDropDown.SelectedValue>0);
}
On the customvalidator above, I purposely left out the 'ControlToValidate' because it throws an error.
Please help.
The easiest would be to include the CustomValidator in the UserControl.
Then you could provide a property for the Validation-Group and another ValidatorEnabled to set the validator group and enable/disable the validator.
Related
I'm having a problem with validation message errors for web forms. I need to get an error when both statements are true(both fields are empty). When I change statement && for || I'm able to get an error but that's not what I want.Thank you. Here is my C# code
protected void CustomValidatorForm_ServerValidate(object source, ServerValidateEventArgs args)
{
if (string.IsNullOrEmpty(drpState.Text) && string.IsNullOrEmpty(txtRegion.Text))
args.IsValid = false;
else
{
args.IsValid = true;
}
}
I'm Trying to run this code for my forms:
<asp:DropDownList ID="drpState" runat="server" CausesValidation="True">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="IL">Illinois</asp:ListItem>
<asp:ListItem Value="IN">Indiana</asp:ListItem>
<asp:ListItem Value="IA">Iowa</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtRegion" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
</div>
<asp:CustomValidator ID="CustomValidatorList" runat="server"
ControlToValidate ="drpState" OnServerValidate="CustomValidatorForm_ServerValidate"
ErrorMessage="At least one of the field need to be filled out" Display="Dynamic"
ForeColor="Red"
>
</asp:CustomValidator>
<asp:CustomValidator ID="CustomValidatorForm" runat="server"
ControlToValidate ="txtRegion" OnServerValidate="CustomValidatorForm_ServerValidate"
ErrorMessage="At least one of the field need to be filled out" Display="Dynamic"
ForeColor="Red"
>
</asp:CustomValidator>
try using like this ,
<asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ErrorMessage="*" ControlToValidate="txtFrom" runat="server" Display="Dynamic" ForeColor="Red" />
Easy way to do this,
After you create a <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
type <asp:RequiredFieldValidator and press Tab Button twice,
You can also give any ErrorMessage as well as define color of the ErrorMessage
It turns out I just needed to add this property ValidateEmptyText="True" and set it to true. This is how validator should look like:
<asp:CustomValidator ID="CustomValidatorList" runat="server"
ControlToValidate ="drpState" OnServerValidate="CustomValidatorForm_ServerValidate"
ErrorMessage="At least one of the field need to be filled out" Display="Dynamic"
ForeColor="Red" ValidateEmptyText="True">
</asp:CustomValidator>
i have two asp:LinkButton and one asp:TextBox + one asp:RegularExpressionValidator on my page.
<asp:LinkButton runat="server" ID="lbNearEventSearch" Text="<%# NearEventText %>" OnClick="NearEventSearch_Click" />
<asp:TextBox runat="server" ID="tbEventSearch" onfocus="clearIt(this)" onblur="setIt(this)" CssClass="addontextfield" MaxLength="5" />
<asp:LinkButton runat="server" ID="lbEventSearch" CssClass="addonbutton" OnClick="EventSearch_Click" />
<asp:RegularExpressionValidator runat="server" ID="EventSearchValidator" ControlToValidate="tbEventSearch" Enabled="false" ErrorMessage="<%# ErrorMessageText %>" ValidationExpression="[0-9]{4}" Display="Dynamic" />
i'd like to restrict the validator to only run when the user clicks on the "lbEventSearch" button and do nothing when the other button is clicked.
on pageload there is no way to know witch button was clicked, right? And the onclick callbacks fire after the validator.
All I can think of is disable the validator and enable it inside the onclick callbacks.
but I wonder if there would be a better way.
thanks
Just set CausesValidation="false" on lbNearEventSearch.
You can use ValidationGroup for this:
<asp:TextBox runat="server" ID="tbEventSearch" onfocus="clearIt(this)" onblur="setIt(this)" CssClass="addontextfield" MaxLength="5" />
<asp:LinkButton runat="server" ID="lbEventSearch" CssClass="addonbutton" OnClick="EventSearch_Click"
ValidationGroup="SearchVG" />
<asp:RegularExpressionValidator runat="server" ID="EventSearchValidator" ControlToValidate="tbEventSearch" Enabled="false" ErrorMessage="<%# ErrorMessageText %>" ValidationExpression="[0-9]{4}" Display="Dynamic"
ValidationGroup="SearchVG" />
Now only controls with specified group (in this case lbEventSearch) will trigger validation on the corresponding validators.
Just give same validationGroup to all elements and lbEventSearch button and dont set validationGroup to other buttons. you can set validation group from property panel
On my admin pages I've got a "Log out" button. On one page there's this form you can fill in and submit and it has some validator controls. The problem is that when I am on this page, I can't log out, because it wants the textboxes to be filled in, even though it's - obviously - not submitted via the log out button. Are the validators executing everytime you try to leave this page, even though I'm not trying to submit a form? To be clear: it works for every other page, it's just the validation here that stops it.
Form code:
<p>
<asp:Label ID="lblA" runat="server" Text="LabelA"></asp:Label><br />
<asp:TextBox ID="txtA" runat="server"></asp:TextBox>
*
<asp:RequiredFieldValidator
ID="rfvA"
runat="server"
ControlToValidate="txtA"
ErrorMessage="Required"
Display="Dynamic">
</asp:RequiredFieldValidator>
</p>
<p>
<asp:Label ID="lblB" runat="server" Text="LabelB"></asp:Label><br />
<asp:TextBox ID="txtB" runat="server"></asp:TextBox>
*
<asp:RequiredFieldValidator
ID="rfvB"
runat="server"
ControlToValidate="txtB"
ErrorMessage="Required"
Display="Dynamic">
</asp:RequiredFieldValidator>
</p>
Logout button:
<asp:Button runat="server" Text="Log out" ID="btnLogout" OnClick="btnLogout_Click"/>
When Log out is clicked:
protected void btnLogout_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
}
Any ideas?
add cause validation false to your button
<asp:Button runat="server" CausesValidation="False" Text="Log out" ID="btnLogout" OnClick="btnLogout_Click"/>
Also you can use the ValidationGroup property of the controls.
Assign the same ValidationGroup to the group of controls(and RequiredFieldValidator) which you require for validation like username and password text boxes and login button(In your case to the two text boxes) and assign the different ValidationGroup to the other controls(In your case to logout button).
So the validation fires only when the same validation group's button has been clicked.
I want to add a confirmation box.
The following code shows the yes / no confirm box, but this does not take care of validation.
If I have made the company name field compulsory, it enters a record, even when I have not entered the company name.
I have called this method in pageload event:
CreateConfirmBox(btnAddEnquiry, "Do You Really Want to Add ?");
Method definitions:
public void CreateConfirmBox(System.Web.UI.WebControls.Button btn, string strMessage)
{
btn.Attributes.Add("OnClick", "return confirm('" + strMessage + "');");
}
aspx file
<asp:Button ID="btnAddEnquiry" runat="server"
BackColor="#0000FF"
ForeColor="LightSlateGray"
OnClick="btnAddEnquiry_Click"
Text="Add Enquiry" Width="154px" />
You are not handling any validations in your code.
I strongly suggest you to use the .NET Validation controls. They are good and barely have to write any code.
More info here;
http://msdn.microsoft.com/en-us/library/bwd43d0x%28v=vs.100%29.aspx
Code Example;
<asp:TextBox ID="TextBox1" runat="server" />
<asp:RequiredFieldValidator IdD="RequiredFieldValidator2"
ControlToValidate="TextBox1"
Display="Static"
Width="100%" runat="server">
*
</asp:RequiredFieldValidator>
<asp:Button ID="btn_Save"
Text="Validate"
OnClick="btnSave_Click"
runat="server" />
If you don't want to implement validators and use only a confirm box and thus keeping the same functionality that you currently have, you can do so by using the OnCientClick of the button which will stop the PostBack if the user clicks No.
<asp:Button ID="btnAddEnquiry" runat="server"
BackColor="#0000FF"
ForeColor="LightSlateGray"
OnClick="btnAddEnquiry_Click"
OnClientClient="javascript: return confirm('Do You Really Want to Add ?');"
Text="Add Enquiry" Width="154px" />
I have an issue with causes validation on my link button in my aspx page. What I have is a page with two text boxes, some validation controls and a submit button. The validation controls have enable client script set to true, and have validation groups. My asp image button has a validation group assigned and causes validation equal to true. I have the focus set to my first text box.
My link button is a click here to learn more about..... and I've specified the validation group and set causes validation to false. In Chrome, everything works fine. In IE, when either clicking any where on the page besides the submit button, or when clicking my link button, my first text box is validated and the error is presented. Now, if I click on the link button again, I am redirected to the page I desire.
This seems a bit weird to me, any one have any ideas?
So, how do I fix this, so IE won't validate until only the submit button is clicked.
Here is what my code looks like:
<span>Value 1:</span>
<asp:TextBox ID="Value1TextBox" runat="server" />
<asp:RequiredFieldValidator ID="Value1_1_Validator" EnableClientScript="true" ControlToValidate="Value1TextBox" Display="Dynamic" ForeColor="Red" Text="is required" ValidationGroup="MyValGroup" runat="server" />
<asp:RegularExpressionValidator ID="Value1_2_Validator" EnableClientScript="true" ControlToValidate="Value1TextBox" ValidationExpression="\somevalexpression\" Display="Dynamic" ForeColor="Red" Text="is not valid" ValidationGroup="MyValGroup" runat="server" />
...
<span>Value 2:</span>
<asp:TextBox ID="Value2TextBox" runat="server" />
<asp:RequiredFieldValidator ID="Value2_1_Validator" EnableClientScript="true" ControlToValidate="Value2TextBox" Display="Dynamic" ForeColor="Red" Text="is required" ValidationGroup="MyValGroup" runat="server" />
...
...
<asp:ImageButton ID="SubmitButton" ImageUrl="~/Images/submit.png" OnClick="SubmitButton_Click" ValidationGroup="MyValGroup" CausesValidation="true" runat="server" />
...
<asp:LinkButton ID="OurPolicyLink" PostBackUrl="~/Policy.aspx" ValidationGroup="MyValGroup" CausesValidation="false" Text="click here" runat="server" />
I came up with a fix using jquery and javascript, its not as clean, but it does work. I would have thought just doing causes validation equal to false would be enough. Perhaps I was wrong.
I have a script javascript file, here is what is looks like.
$(function () {
$"[id$=_Value1TextBox]").focus();
$("[id$=_Validator]").each(function () {
$(this)[0].enabled = false;
});
$("[id$=_SubmitButton]").click(function () {
$("[id$=_Validator]").each(function () {
$(this)[0].enabled = true;
});
Page_ClientValidate("MyValGroup");
if (!Page_IsValid) {
return false;
}
return true;
});
});
It is because you are setting the focus on the text box. When the focus leaves the text box it is triggering validation. Either don't set the focus on the text box or set CuasesValidation=False on the TextBox. The problem with latter option is that it won't validate the text box until something else in the same validation group triggers the validation.