How to create a custom validator in vb.net
Hi!
I just have this program that validates if the selected file is an excel file, and I want this to have a separate function for validation so I can use that function to other program, can you please give me an example on how to do this function..
Thanks
jbutardo
Junior Poster in Training
73 posts since Jan 2012
Reputation Points: 8
Solved Threads: 1
Why cant you force the user to select only excel file? using fileopen Dialogue?
are you opening the file through program not using fileopen Dialogue?
Yep, I am using fileUpload
jbutardo
Junior Poster in Training
73 posts since Jan 2012
Reputation Points: 8
Solved Threads: 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
With New OpenFileDialog
.Filter = "Only myCool Excel Files|*.xls;*.xlst"
If .ShowDialog = Windows.Forms.DialogResult.OK Then
MsgBox("tada")
End If
End With
End Sub
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
Pgmer, why Not use IO.Path.GetExtension("full File.Path or just File.Name here") ?
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
Is it web application? Can you please post your code here what you have so far?
here is my code:
Dim fileName As String
Dim fileExtension As String
Dim fileServer As String
fileName = FileUpload1.PostedFile.FileName
fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName)
fileServer = FileUpload1.FileName
If FileUpload1.HasFile = True Then
' some code here
If fileExtension.Equals(".xls") = True Then
'some code here
else
'some code here
end if
else if FileUpload1.hasFile = false then
'some code here
end if
here it is.. I want this to be a clean code, rather than this one, and I want to know how can this be converted to n-layer.. thanks
jbutardo
Junior Poster in Training
73 posts since Jan 2012
Reputation Points: 8
Solved Threads: 1
Pgmer, something else other than glad I could help.
.You can use more than one "something" in a Select Case(line.2).
Select Case fileExtenstion
Case ".xlt",".xls"
Return True
Case Else
Return False
End Select
jbutardo, I already flagged your thread as an ASP.NET thread, though I will try to help once you provide more defined details as to exactly "how" to make it more of a "clean code" other than having one too many declarations.
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
If fileExtension.Equals(".xls") = True Then'some code here else'some code hereend if
what is the problem with this code? you want to make genreal function to check or validate .xls?
yes. i want this to become a general function, can you help me on how to do it? thanks
jbutardo
Junior Poster in Training
73 posts since Jan 2012
Reputation Points: 8
Solved Threads: 1
See if this helps.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fileName As String = FileUpload1.PostedFile.FileName
Dim fileServer As String = FileUpload1.FileName
If FileUpload1.HasFile Then
' some code here
'//////////////////////////////////////////////////
If isCoolExcelFile(fileName) Then
'//////////////////////////////////////////////////
'some code here
Else
'some code here
End If
Else '// If FileUpload1.Has "NO" File
'some code here
End If
End Sub
Private Function isCoolExcelFile(ByVal selCoolFile As String) As Boolean
Select Case IO.Path.GetExtension(selCoolFile)
Case ".xls"
Return True
Case Else
Return False
End Select
End Function
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
See if this helps.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fileName As String = FileUpload1.PostedFile.FileName
Dim fileServer As String = FileUpload1.FileName
If FileUpload1.HasFile Then
' some code here
'//////////////////////////////////////////////////
If isCoolExcelFile(fileName) Then
'//////////////////////////////////////////////////
'some code here
Else
'some code here
End If
Else '// If FileUpload1.Has "NO" File
'some code here
End If
End Sub
Private Function isCoolExcelFile(ByVal selCoolFile As String) As Boolean
Select Case IO.Path.GetExtension(selCoolFile)
Case ".xls"
Return True
Case Else
Return False
End Select
End Function
Okay, thanks for the help..
jbutardo
Junior Poster in Training
73 posts since Jan 2012
Reputation Points: 8
Solved Threads: 1