File extension validation Javascript

You can use this function to validate the extension of a file, for client side file upload checkes.

// You can specify the file type in the code "valid_extensions = /(.doc|.pdf)$/i;" . 
//If you want to add .docx supprt simply add |.docx (ex:(.doc|.pdf |.docx| .your-extension) ) 

var valid_extensions = /(.doc|.pdf)$/i;
function CheckExtension(fld)
{
	if(fld.value) {
		if (valid_extensions.test(fld.value)){
			return true;
		} else {
			return false;
		}
	} else {
		return true;
	}	
}

You may also like...

3 Responses

  1. shaswati says:

    Its really helpful for me……….Thank u.

  2. chetan says:

    thankx for code…..short n very useful

  3. Prashant says:

    Thanks a lot… saved lot of my efforts

Leave a Reply

Your email address will not be published. Required fields are marked *