Validation Web controls are ASP.NET 2.0 Web controls designed to validate form fields control inputs with some rules apply to it. Previously it used to achieve with the javascript/vbscripts functions on user controls like text box and drop down list. it was bit annoying and time consuming and also code grows to longer line which is difficult to manage.
asp.net 2.0 introduces new validation controls to resolve these issues. One of which I am discussing here,
CustomValidator- Checks the form field's value against custom validation logic that you, the developer, provide with the help of client script written in javascript.
javascript function
function check_ClientValidate(source, args)
{
var _pageStatus = true;
if(args.Value != "")
{ _pageStatus = true;}
else
{
con = false;
document.getElementById("CustomValidator1").innerHTML = "Please Select file!";
}
if( _pageStatus == true)
{
var file1 = args.Value.substring(args.Value.length - 3, args.Value.length); //doc
if( file1 == "doc")
{
_pageStatus = true;
}
else
{
_pageStatus = false;
document.getElementById("CustomValidator1").innerHTML = "You can upload doc file only.";
}
}
if( _pageStatus == true)
{ args.IsValid = true;}
else
{ args.IsValid = false;}
}
You should also provide server validation code as well to perform both ways. this is to minimize error on server side in case client side validation get failed. [ techincal reasons..] Implement same logic here in server validation function to avoid runtime error.
asp.net page controls .aspx.vb file
Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
Dim _pageStatus As Boolean = True
If args.Value <> ("") Then
_pageStatus = True
Else
_pageStatus = False
CustomValidator1.Text = "Please select File!"
End If
If _pageStatus Then
Dim file1 As String = args.Value.Substring(args.Value.Length - 3, args.Value.Length)
If file1.Equals("doc") Then
_pageStatus = True
Else
_pageStatus = False
CustomValidator1.Text = "You can upload file such as .doc"
End If
End If
If _pageStatus Then
args.IsValid = True
Else
args.IsValid = False
End If
End Sub
this will solve your problem of file upload with limited extension.
No comments:
Post a Comment