Quick Search

Client Side Validation expression for File

February 27th, 2010 by admin
Leave a reply »

 

If you want to validate your file extension on client site before upload the file. You can easily do this using regular expression Validator. Here’s the example to allow uploading only .jpg or .gif files. All you need is a FileUpload control and a Regular Expression Validator to check file type.

              <asp:RegularExpressionValidator ID=”RegularExpressionValidator2″ ControlToValidate=”FileUpload1″ Display=”dynamic” runat=”server” ValidationExpression=”^.+(.jpg|.JPG|.gif|.GIF)$” ErrorMessage=”Upload a valid file”><asp:RegularExpressionValidator>

 
You can change the file extension as you want for do this you just replace the file extension with your file type.
 
JavaScript Validation Expression For File
You can also validate your file with JavaScript using following function :
 
function validate_file(data){
        data = data.replace(/^\s|\s$/g, “”); //trims string
            return /^[a-z][\w]+\.(asp|html|htm|shtml|php)$/i.test(data)
    }//eof – validate_file

Posted in ASP.NET

You can follow any responses to this entry through the RSS 2.0 Feed. You can leave a response , or trackback from your own site.

Leave a Reply