I need help with a RequiredFieldValidator control in ASP.net 3.5

mainrotor
mainrotor used Ask the Experts™
on
Hi Experts,
I need assistance using a RequiredFieldValidator control in my ASP.Net 3.5 application.  Currently the RequiredFieldValidator is checking to see if the file selected in my FileUpload control contains the words 'Year', and 'Report' in it (see attached code).  

I want the RequiredFieldValidator to make sure (validate) that the file selected in my FileUpload control also contains my DropDownList control's text value (DropDownList.SelectedItem.Text).  How can I do this?

Thanks in advance,
mrotor


<asp:RegularExpressionValidator 
ID="RegularExpressionValidator1" runat="server"
ErrorMessage="Incorrect PSBD12 File."
ControlToValidate="fulPriorYearRSBD12"
ValidationExpression="^(?=.*?Year)(?=.*?PSBD12).*$" Font-Names="Arial" Font-Size="X-Small"/>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
you to have to add a custom validator ..in client function u should check the equality

Author

Commented:
How would I use the CustomValidator deejanbu?  Can you provide an Example?
Most Valuable Expert 2012
Top Expert 2014

Commented:
Using the custom validator control

http://www.4guysfromrolla.com/articles/073102-1.aspx
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Try this

<script type="text/javascript">
		function ValidateFile(oSrc, args)
		{
			var filePath = args.Value;
			var fileName = filePath.substring(filePath.lastIndexOf('\\') + 1);
			var ddl = document.getElementById("<%= ddl.ClientID %>");

			var ddlValue = ddl.options[ddl.selectedIndex].value;
			var ddlText = ddl.options[ddl.selectedIndex].text;

			if (fileName.indexOf("Year") != -1 && fileName.indexOf("Report") != -1 && fileName.indexOf(ddlText) != -1)
			{
				args.IsValid = false;
			}
			else
				args.IsValid = false;
		}
    
	</script>



<asp:FileUpload ID="fupFileToUpload" runat="server" Width="250px" />
						<asp:DropDownList ID="ddl" runat="server">
							<asp:ListItem Text="File 1" Value="A"></asp:ListItem>
							<asp:ListItem Text="File 2" Value="B"></asp:ListItem>
							<asp:ListItem Text="File 3" Value="C"></asp:ListItem>
							<asp:ListItem Text="File 4" Value="D"></asp:ListItem>
						</asp:DropDownList>
						<asp:CustomValidator ID="c" runat="server" ErrorMessage="Invalid File" Text="Required"
							ClientValidationFunction="ValidateFile" ControlToValidate="fupFileToUpload" ValidateEmptyText="true"></asp:CustomValidator>

Open in new window

Commented:
try @baiju_nagori's code and let us know

Author

Commented:
baiju
I get the following error when I try your code:

'ClientID' is not a member of 'System.Web.UI.WebControls.DropDownList'.      

I don't know if this has to do with the fact that I am creating my ddl at runtime and not at designtime like this:

ddl.DataValueField = "ClientID"
ddl.DataTextField = "ClientCode"
ddl.DataSource = GetData()



Also in your code, will the following line of code:
var fileName = filePath.substring(filePath.lastIndexOf('\\') + 1);

be affected if my file paths look like this:
C:\TestSpreadSheet\AnnualRGBReport.xls


Is the line of code expecting the double backslash in the filepath?  ('\\')


Thanks,
mrotor
Can you post entire code along with ASPX?

Author

Commented:
Okay, will do.

Author

Commented:
Baiju, I figured it out.  Here is what I did.  I added the code below to my FileUpload Control source:
                <asp:CustomValidator ID="CustomValidator1" runat="server"
                    ControlToValidate="FileUpload1"
                    ErrorMessage="Incorrect file format."
                    OnServerValidate ="CheckYearValidation">*</asp:CustomValidator>

Then I added a validation procedure in my codebehind:
    Public Sub CheckYearValidation(ByVal source As Object, ByVal args As ServerValidateEventArgs)
         args.IsValid = (args.Value.Contains("Year") _
                        And args.Value.Contains("PSBD12") _
                        And args.Value.Contains(ddlClient.SelectedItem.Text))
    End Sub

That worked.  Thanks baiju for your help.

mrotor


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial