Required Field Validator not Working For DropDownList - asp.net

Why is the validator not working here. It's working fine for other textbox controls though.
There is no error. Its just does the postback.
<asp:DropDownList Id="ddlCustomer" runat="server" AppendDataBoundItems="true"
DataTextField="customername" DataValueField="customerid">
<asp:ListItem Text="--Select--" Value="0"></asp:ListItem>
<asp:ListItem Text=" < Add New > " Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator runat="server" ControlToValidate="ddlCustomer"
cssclass="required" display="dynamic" errormessage="*" setfocusonerror="true"
initialvalue="--Select--"></asp:RequiredFieldValidator>

initialValue should be "0" not the Text "--Select--"
like below
<asp:RequiredFieldValidator runat="server" ControlToValidate="ddlCustomer"
cssclass="required" display="dynamic" errormessage="*" setfocusonerror="true"
initialvalue="0"></asp:RequiredFieldValidator>

You have given the initial value as --Select-- in RequiredFieldValidator but there is no item which has the value as --Select--, RequiredFieldValidtor fires when it get the item's value which is equal to InitialValue. So in this case it will never fire.
You need to put the value as 0 in the InitialValue of validator or change the value of first item of DropDown.
<asp:RequiredFieldValidator runat="server" ControlToValidate="ddlCustomer"
cssclass="required" display="dynamic" errormessage="*" setfocusonerror="true"
InitialValue="0"></asp:RequiredFieldValidator>
OR
<asp:ListItem Text="--Select--" Value="--Select--"></asp:ListItem>
But I'd recommend you to use the first approach.

Related

RequiredFieldValidator Not Working in ComboBox

I think one common problem in using ComboBox is that the RequiredFieldValidator is not working properly on it.
I also tried:
1. CustomValidator
2. CompareValidator
But none of it is working. Please help.
CompareValidator
<asp:CompareValidator runat="server" ID="CompareFieldValidator1"
ErrorMessage="This field cannot be empty" ControlToValidate="CB_Operation"
ValueToCompare="-1" Operator="NotEqual"></asp:CompareValidator>
RequiredFieldValidator
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"ControlToValidate="CB_Indication"
runat="server" ErrorMessage="This field cannot be empty" Font-Names="Tahoma" Font-Size="Small"></asp:RequiredFieldValidator>
Any ideas?
UPDATE: Here is the mark-up of one of my ComboBoxes
<asp:ComboBox ID="CB_Operation" runat="server" Font-Names="Tahoma"
Font-Size="Small" Width="170px" Font-Bold="True"
OnSelectedIndexChanged="CB_Operation_SelectedIndexChanged" AutoPostBack="True" InitialValue="0">
<asp:ListItem Value="0">Add</asp:ListItem>
<asp:ListItem Value="1">Modify</asp:ListItem>
</asp:ComboBox>

Required Field Validator for Dropdown list in ASP.NET

I have the following dropdown in my page
<asp:DropDownList ID="cboEmployerType" runat="server" TabIndex="8" Width="60%" onclick="javascript:shouldsubmit=false;">
<asp:ListItem Value="Null">-Select-</asp:ListItem>
<asp:ListItem Value="E">Employer</asp:ListItem>
<asp:ListItem Value="O">OJT Provider</asp:ListItem>
</asp:DropDownList>
And a RequiredFieldValidator for it
<asp:RequiredFieldValidator ID="cboEmployerType_RequiredFieldValidator" runat="server" InitialValue="null" ErrorMessage="Employer Type Required" ForeColor="Red" Font-Size="0.9em" ControlToValidate="cboEmployerType" ValidationGroup="valEmployer" Display="None"></asp:RequiredFieldValidator>
But I do not get the Validation Message. What am I missing?
You have taken Display="None" in RequiredFieldValidator take it as
Display="Dynamic"
and take InitialValue="Null"
also assign the same validation group to drop down list
i.e. ValidationGroup="valEmployer"
It works. Just make the following 3 changes:
Either remove Display="None" or use Display="Dynamic" in RequiredFieldValidator
Set ValidationGroup="valEmployer" to dropdown as well as the button for which the validation should occur
Set InitialValue="Null" instead of InitialValue="null" in RequiredFieldValidator with capital "N"
The following is the code
<asp:DropDownList ID="cboEmployerType" ValidationGroup="valEmployer" runat="server" TabIndex="8" Width="60%" onclick="javascript:shouldsubmit=false;">
<asp:ListItem Value="Null">-Select-</asp:ListItem>
<asp:ListItem Value="E">Employer</asp:ListItem>
<asp:ListItem Value="O">OJT Provider</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="cboEmployerType_RequiredFieldValidator" runat="server" InitialValue="Null" ErrorMessage="Employer Type Required" ForeColor="Red" Font-Size="0.9em" ControlToValidate="cboEmployerType" ValidationGroup="valEmployer" Display="Dynamic"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button1" ValidationGroup="valEmployer" runat="server" Text="Button" />
Hope this helps.
try with InitialValue
InitialValue="Null" Display="Dynamic"
Remove ValidationGroup="valEmployer" property from the RequiredFieldValidator control
and set InitialValue="Null" instead of "null" in RequiredFieldValidator control.
Set Display=Dynamic.
Try this.
Add ValidationGroup="valEmployer" property in dropdownlist and in the button on click of which validation occurs.
OR
Just remove ValidationGroup="valEmployer" property from the RequiredFieldValidator control.
This is working for me-
<asp:DropDownList ID="cboEmployerType" ValidationGroup="valEmployer" runat="server" TabIndex="8" Width="60%" onclick="javascript:shouldsubmit=false;">
<asp:ListItem Value="Null">-Select-</asp:ListItem>
<asp:ListItem Value="E">Employer</asp:ListItem>
<asp:ListItem Value="O">OJT Provider</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="cboEmployerType_RequiredFieldValidator" ValidationGroup="valEmployer" runat="server" InitialValue="Null" ErrorMessage="Employer Type Required" ForeColor="Red" Font-Size="0.9em" ControlToValidate="cboEmployerType" Display="Dynamic"></asp:RequiredFieldValidator>
Keep ValidationGroup="valEmployer" on which event you want to fire the Validations.

ASP:TextBox or ASP:CheckBoxList, throwing Javascript errors when Requiredfield validator is there

I have created a User Control for my SharePoint, which has a simple TextBox and a CheckBoxList. For both of these controls, I have ASP:RequiredFieldValidator and ASP:RegularExpressionValidator.
When I select some item in the CheckBoxList or type some input in the TextBox, I am getting a javascript error in some unknown location. (This is not accepted by my customer.)
Now, when I debug this using FireBug, in the console I see validators[i] is null. It is actually failing in the JS code generated by these validators.
Can some one help me?
Edited:
<asp:TextBox ID="txtNumbers" runat="server" CssClass="Label4" ></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidatorGPC" runat="server"
ControlToValidate="txtNumbers" ErrorMessage="Only Numbers Accepted "
ValidationExpression="^\d+$"
SetFocusOnError="true"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredNUMValue" runat="server" ControlToValidate="txtNumbers"
SetFocusOnError="true" ErrorMessage="Please enter a valid number"></asp:RequiredFieldValidator>
Also,
I added required field validators for RadioButtonList (not CheckBoxList).
<asp:RadioButtonList ID="rbtOptions" runat="server" CssClass="Label3">
<asp:ListItem Text="Option 1" />
<asp:ListItem Text="Option 2" />
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="rbtOptions"
SetFocusOnError="true" ErrorMessage="Please Select a Valid Option"></asp:RequiredFieldValidator>
When I either input any text in the TextBox or select any option, there is a JavaScript error.
There is something else on your page that is conflicting with this. Taking what you posted and trying a simple page it works as it should, so I'm assuming that there's other elements / scripts that are causing this.
<div>
<asp:TextBox ID="txtNumbers" runat="server" CssClass="Label4"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidatorGPC" runat="server" ControlToValidate="txtNumbers" ErrorMessage="Only Numbers Accepted " ValidationExpression="^\d+$" SetFocusOnError="true"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredNUMValue" runat="server" ControlToValidate="txtNumbers" SetFocusOnError="true" ErrorMessage="Please enter a valid number"></asp:RequiredFieldValidator>
<asp:RadioButtonList ID="rbtOptions" runat="server" CssClass="Label3">
<asp:ListItem Text="Option 1" />
<asp:ListItem Text="Option 2" />
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="rbtOptions" SetFocusOnError="true" ErrorMessage="Please Select a Valid Option"></asp:RequiredFieldValidator>
<asp:Button ID="btnSubmit" runat="server" Text="submit" />
</div>
Do you have any script tags, or other elements in your markup that could be interfering with this?
Self closed tags can cause issues like this ( is a no no)

asp.net Validation on DropDownList box

I have a dropdownlist (cboViewAlbums) which has displays album values. The first item is a
Please select an album.... I am trying to use validation which when lb_create_album linkButton is clicked throws an error if the cboViewAlbums list has the value 0 selected.
Below is the code for the this and my attempt:
<asp:DropDownList ID="cboViewAlbums" runat="server"
DataSourceID="SqlDataSource1" DataTextField="album_name"
DataValueField="album_id" Width="250px" AutoPostBack="True" AppendDataBoundItems="true">
<asp:ListItem Value="0">Please select an album...</asp:ListItem>
</asp:DropDownList>
<asp:LinkButton ID="lb_create_album" runat="server">Create Album</asp:LinkButton>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:fpaConnectionString %>"
SelectCommand="SELECT [album_id], [album_name] FROM [fpa_albums] ORDER BY [album_name]">
</asp:SqlDataSource>
<br />
<asp:HyperLink CssClass="example7" ID="hLinkUploadPhotos" NavigateUrl="multiple_upload.aspx" runat="server">Upload Multiple Photos</asp:HyperLink>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="cboViewAlbums" ErrorMessage="Please Select an Album"
InitialValue="Please select an album..."></asp:RequiredFieldValidator>
Any idea how I can get this working?
Thanks
You have to use Range validator with dropdown list & set mininmum value greater then 0 & maximum value to set any max value , aslo provide type value of min & max and that is integer.
Below is the sample code i have make for you you have to bind your datasource insted of static list items.
<asp:DropDownList runat="server" ID="ddl1" >
<asp:ListItem Value="0" Text="Select value" />
<asp:ListItem Value="1" Text="text1" />
<asp:ListItem Value="2" Text="text2" />
</asp:DropDownList>
<asp:RangeValidator ErrorMessage="Please select value" ControlToValidate="ddl1" runat="server"
MinimumValue="1" MaximumValue="100000000" Type=Integer />
<asp:Button Text="text" runat="server" />
If this helpful to you please mark as an answer
Thanks
Arun
First, you can't do validation with HyperLink, better use the LinkButton. HyperLink does not do post back, so that's your first error.
Second, on your RequiredFieldValidator, put the initialvalue=0, and that should fix your problem.
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="cboViewAlbums" ErrorMessage="Please Select an Album"
InitialValue="0"></asp:RequiredFieldValidator>

How to add a RequiredFieldValidator to DropDownList control?

I have a DropDownList binded with aSqlDataSource to display the values from the database.
I am unable to validate using a RequiredFieldValidator.
For the most part you treat it as if you are validating any other kind of control but use the InitialValue property of the required field validator.
<asp:RequiredFieldValidator ID="rfv1" runat="server" ControlToValidate="your-dropdownlist" InitialValue="Please select" ErrorMessage="Please select something" />
Basically what it's saying is that validation will succeed if any other value than the 1 set in InitialValue is selected in the dropdownlist.
If databinding you will need to insert the "Please select" value afterwards as follows
this.ddl1.Items.Insert(0, "Please select");
Suppose your drop down list is:
<asp:DropDownList runat="server" id="ddl">
<asp:ListItem Value="0" text="Select a Value">
....
</asp:DropDownList>
There are two ways:
<asp:RequiredFieldValidator ID="re1" runat="Server" InitialValue="0" />
the 2nd way is to use a compare validator:
<asp:CompareValidator ID="re1" runat="Server" ValueToCompare="0" ControlToCompare="ddl" Operator="Equal" />
If you are using a data source, here's another way to do it without code behind.
Note the following key points:
The ListItem of Value="0" is on the source page, not added in code
The ListItem in the source will be overwritten if you don't include
AppendDataBoundItems="true" in the DropDownList
InitialValue="0" tells the validator that this is the value that
should fire that validator (as pointed out in other answers)
Example:
<asp:DropDownList ID="ddlType" runat="server" DataSourceID="sdsType"
DataValueField="ID" DataTextField="Name" AppendDataBoundItems="true">
<asp:ListItem Value="0" Text="--Please Select--" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvType" runat="server" ControlToValidate="ddlType"
InitialValue="0" ErrorMessage="Type required"></asp:RequiredFieldValidator>
<asp:SqlDataSource ID="sdsType" runat="server"
ConnectionString='<%$ ConnectionStrings:TESTConnectionString %>'
SelectCommand="SELECT ID, Name FROM Type"></asp:SqlDataSource>
InitialValue="0" : initial validation will fire when 0th index item is selected in ddl.
<asp:RequiredFieldValidator InitialValue="0" Display="Dynamic" CssClass="error" runat="server" ID="your_id" ValidationGroup="validationgroup" ControlToValidate="your_dropdownlist_id" />

Resources