customvalidator onServerValidate not firing - c#

I have a Radio button List and Text Box both with validation.
<asp:RadioButtonList ID="member" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
<asp:requiredfieldvalidator id="unionvalidator" runat="server" controltovalidate="member" errormessage="Required" />
Required if member == "yes"
<asp:TextBox runat="server" ID="union"></asp:TextBox>
<asp:customvalidator ID="Customvalidator1" runat="server" ValidateEmptyText="true" onServerValidate="UnionValidate" errormessage="Your current union is required" />
My ServerValidate which doesn't fire at all.
public void UnionValidate(object source, ServerValidateEventArgs args)
{
if (member.Text == "yes" && union.Text.Trim() == "")
args.IsValid = false;
}

Are you calling the Page.Validate() method somewhere in your code behind or does the submit button has the CausesValidation set to true?

Related

If condition for required field validation

I have a DropDownList and a TextBox in c#.net. If DropDownList value is "No" then some value must enter in TextBox. If dropdownlist value is yes then required field validator is not needed for that TextBox. How to make it possible?
<asp:DropDownList ID="dropdownlist1" runat="server"
CssClass="NormalText" Width="155px" AutoPostBack="true"
onselectedindexchanged="ddls_SelectedIndexChanged">
<asp:ListItem Selected="True" Value=""></asp:ListItem>
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:TextBox ID="Textbox1" runat="server" CausesValidation="True"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="Textbox1" ErrorMessage="Explanation needed If you select NO">
</asp:RequiredFieldValidator>
</td>
protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
}
Use this code for conditional validation:
protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
if(dropdownlist1.SelectedValue == "No")
{
RequiredFieldValidator1.Enabled = true;
}
else if(dropdownlist1.SelectedValue == "Yes")
{
RequiredFieldValidator1.Enabled = false;
}
}
Optionally, you may also have to set Submit Button Validation group:
ButtonSubmit.ValidationGroup = string.Empty;
when field validator is not enabled.

Disable RequiredFieldValidator in asp.net c#

I am trying to disable a RequiredFieldValidator by radio button list value. I have it coded but it doesn't seems to be working.
What i have done so far:
protected void radioButtonList(object sender, EventArgs e)
{
if (((RadioButtonList)dvInsertPromotion.FindControl("RadioButtonList2")).SelectedValue == "Y")
{
((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate1")).Enabled = false;
((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate")).Enabled = false;
this.addPromotion.Show();
}
else
{
((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate1")).Enabled = true;
((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate")).Enabled = true;
this.addPromotion.Show();
}
}
html:
<asp:RadioButtonList ID="RadioButtonList2" runat="server" ValidationGroup="addValidationGp" OnSelectedIndexChanged="radioButtonList">
<asp:ListItem Text="Yes" Value="Y"></asp:ListItem>
<asp:ListItem Text="No" Value="N" Selected></asp:ListItem>
</asp:RadioButtonList>
<asp:TextBox ID="txtPubDate" Width="75" MaxLength="10" runat="server" AutoPostBack="true" OnTextChanged="insertStartEndDateTime_SelectedIndexChanged"/>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtPubDate"
PopupPosition="Right" Format="dd/MM/yyyy" />
<asp:RequiredFieldValidator ID="rfvdate" ValidationGroup="addValidationGp" runat="server"
ControlToValidate="txtPubDate" ErrorMessage="*" Display="Dynamic" Enabled="true"
SetFocusOnError="true" /><br />
I've had the same problem in the past and the only way I was able to solve this was by making the validators invisible altogether:
((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate1")).Visible = false;
Just don't forget to set them to visible again whenever you want to enable them.

Require TextBox depending on RadioButtonList selection in Repeater

I have a repeater with a RadioButtonList and TextBox inside. I want the TextBox to be required only if the RadioButtonList has a value of 1 or 2. How can I achieve this?
I thought I could initially enable the RFV on a selected index change but it can't see the items since it is within a repeater. Do I need to do this within the ItemDataBound?
<asp:Repeater ID="repeaterSurvey" runat="server">
<ItemTemplate>
<div class="form-group">
<asp:Label ID="labelQuestion" runat="server" Text='<%# Eval("Question")%>' />
<asp:RequiredFieldValidator ID="RFV1" CssClass="required" runat="server"
ErrorMessage="Required" Display="Dynamic" ControlToValidate="surveyList" />
<asp:RadioButtonList RepeatDirection="Horizontal" ID="surveyList" AutoPostBack="true"
OnSelectedIndexChanged="surveyList_SelectedIndexChanged" runat="server">
<asp:ListItem Value="1">Strongly Disagree</asp:ListItem>
<asp:ListItem Value="2">Disagree</asp:ListItem>
<asp:ListItem Value="3">Agree</asp:ListItem>
<asp:ListItem Value="4">Strongly Agree</asp:ListItem>
<asp:ListItem Value="0">N/A</asp:ListItem>
</asp:RadioButtonList>
<asp:HiddenField ID="hiddenfieldID" Value='<%# Eval("ID")%>' runat="server" />
<asp:TextBox ID="textboxComment" Placeholder="Comments" CssClass="form-control" TextMode="MultiLine"
Rows="3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RFV2" Enabled="false" CssClass="required" runat="server"
ErrorMessage="Comment Required" Display="Dynamic" ControlToValidate="textboxComment" />
</div>
</ItemTemplate>
</asp:Repeater>
Codebehind
protected void surveyList_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (surveyList.SelectedValue == "1" || surveyList.SelectedValue == "2")
{
RFV2.Enabled = true;
}
else
{
RFV2.Enabled = false;
}
}
With the help of the comment section (stephen.vakil) I was able to create a solution to my problem:
protected void surveyList_SelectedIndexChanged(object sender, System.EventArgs e)
{
RadioButtonList surveyList = (RadioButtonList)sender;
string value = surveyList.SelectedValue;
RepeaterItem row = (RepeaterItem)surveyList.NamingContainer;
if (value == "1" || value == "2")
{
((RequiredFieldValidator)row.FindControl("RFV2")).Enabled = true;
}
else
{
((RequiredFieldValidator)row.FindControl("RFV2")).Enabled = false;
}
}

required field validator with javascript confirm function not validating

I am using required field validator, which validates text box and on click of submit button i have to ask for confirm, and I use confirm() function of java script.
Issue is that when I press OK in confirmation box page post backs and required field validator does not stops the page when I left the text box empty.
After reading posts from stackoverflow I used custom validator to stop the page, but I could not here is the code.
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1" ValidationGroup="one" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="one"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick="validate();" ValidationGroup="one" onclick="Button1_Click"/>
<asp:CustomValidator ID="CustomValidator1" ValidateEmptyText="true" runat="server" ValidationGroup="one" ErrorMessage="CustomValidator"></asp:CustomValidator>
<script type='text/javascript'>
function validate() {
var cv = document.getElementById('MainContent_CustomValidator1');
if (cv) {
cv.isValid = confirm('are you sure want to update record ?');
}
} </script>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1" ValidationGroup="one" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="one"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick="return validate();" ValidationGroup="one" onclick="Button1_Click"/>
<asp:CustomValidator ID="CustomValidator1" ValidateEmptyText="true" runat="server" ValidationGroup="one" ErrorMessage="CustomValidator" ClientValidationFunction="TextBox1Client"></asp:CustomValidator>
<script type='text/javascript'>
function validate() {
if(confirm('are you sure want to update record ?')){
return true;
}
else
{
return false;
}
}
//you need to add a custom validot client function also
function TextBox1Client(sender, args) {
//write your custom code here
args.IsValid = false;
//OR
args.IsValid =true;
}
</script>
try this code use Page_ClientValidate
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick="Javascript:if(Page_ClientValidate('one')){return validate();}" ValidationGroup="one" onclick="Button1_Click"/>
correct your code like this
if(confirm('are you sure want to update record ?')==false){
return false;
}
Thanks.
JavaScript is case sensitive, try this:
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1" ValidationGroup="one" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="one"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick="validate();" ValidationGroup="one" onclick="Button1_Click"/>
<asp:CustomValidator ID="CustomValidator1" ValidateEmptyText="true" runat="server" ValidationGroup="one" ErrorMessage="CustomValidator"></asp:CustomValidator>
<script type='text/javascript'>
function validate() {
var cv = document.getElementById('MainContent_CustomValidator1');
if (cv) {
cv.IsValid = confirm('Are you sure want to update record?');
}
} </script>
You didn't capitalize the i in IsValid.

previous button shows validations

This is an example of my textbox in my form with a custom validation, I want to disable the validators :
<asp:textbox id="txtFirstName" runat="server" /></td><td>
<script language ="javascript">
function requireFirstName(source, args) {
if (document.getElementById("<%=txtFirstName.ClientID %>").value =="") {
args.IsValid = false;
}
else {
args.IsValid = true;
}
}
</script>
<asp:CustomValidator ID="RequiredValidator1" runat="server"
ErrorMessage="You must enter your first name."
ForeColor="Red" clientvalidationfunction="requireFirstName"
></asp:CustomValidator>
This is my previous button:
<asp:Button ID="PreviousButton" runat="server"
Text="<-- Back to Instructions" OnClick="btnBack_Click"
class="previous" />
<script type="javascript">
document.getElementById("<%=PreviousButton.ClientID%>").disableValidation = true;
</script>
This will not work and still has validators..help
Try setting CausesValidation to false.
<asp:Button ID="PreviousButton" runat="server" Text="<-- Back to Instructions" OnClick="btnBack_Click" class="previous" CausesValidation="false" />
<asp:Button ID="PreviousButton" runat="server" CausesValidation="false"
Text="<-- Back to Instructions" OnClick="btnBack_Click"
class="previous" EnableClientScript="false" />

Categories