validation For Uploading File Using javascript Extension checking
For This we use following regular Expression According To need.....
Regular expression to validate file formats for .mp3 or .MP3 or .mpeg or .MPEG or m3u or M3U
Re= /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.mp3|.MP3|.mpeg|.MPEG|.m3u|.M3U)$/;
Regular expression to validate file formats for .doc or .docx
Re= /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.doc|.docx|.DOC|.DOCX)$/;
Regular expression to validate file formats for .txt or .TXT
Re= /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.txt|.TXT)$/;
Regular expression to validate file formats for .jpeg or .JPEG or .gif or .GIF or .png or .PNG
Re= /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpeg|.JPEG|.gif|.GIF| .png|.PNG)$/;
function upload() {
var uploadcontrol = document.getElementById('<%=FileUpload1.ClientID%>').value;
//Regular Expression for fileupload control.
var reg = /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.doc|.docx|.DOC|.DOCX)$/;
if (uploadcontrol.length > 0) {
//Checks with the control value.
if (reg.test(uploadcontrol)) {
return true;
}
else {
//If the condition not satisfied shows error message.
alert("Only .doc, docx files are allowed!");
return false;
}
}
}
and Write on texrbox code also...
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="upload" OnClientClick="return upload()" />
No comments:
Post a Comment