开发者

Cross browser File input validation in asp.net 2.0

开发者 https://www.devze.com 2023-03-04 15:17 出处:网络
input type file or the file up开发者_高级运维load html control/ asp.net control value seems to change depending on browsers.

input type file or the file up开发者_高级运维load html control/ asp.net control value seems to change depending on browsers.

<input type="file" id="fileUpload" name="fileUpload" />

Chrome: fileUpload.value gives c:\fakePath\filename

Firefox: fileUpload.value gives filename.ext

ie: fileUpload.value gives Full path+filename

i used a regular expression validator with validation expression as below

^[a-za-zA-Z0-9_\.]{3,28}\.(pdf|txt|doc|docx|png|gif|jpeg|jpg|zip|rar)$

carries out below process:

  1. File name must be 3 to 28 characters

2.Extension must match the group only.

Since the value of fileUpload control is different in different browsers how do i validate it now??


If you only use standard RegularExpressionValidator then it will work cross browser itself. Because RegularExpressionValidator works client side too when you select a file it automatically display the ErrorMessage if the file is invalid.

Sample Code

<asp:FileUpload ID="fup" runat="server" />        
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
ControlToValidate="fup" ErrorMessage="Invalid File" 
ValidationExpression="^[a-za-zA-Z0-9_\.]{3,28}\.(pdf|txt|doc|docx|png|gif|jpeg|jpg|zip|rar)$"></asp:RegularExpressionValidator>


@Fraz Sundal couldn't comment with the code hence the new post @Fraz still no clue why it fails here is the code snippet<asp:RegularExpressionValidator id="rgvFile" runat="server" font-bold="true" errormessage="Only pdf,doc,zip,jpeg,png,gif files allowed" cssclass="rgvfile" enableclientscript="true" display="Dynamic" controltovalidate="fileUpload" validationexpression="^[a-za-zA-Z0-9_\.]{3,28}\.(pdf|txt|doc|docx|png|gif|jpeg|jpg|zip|rar)$" text="Only pdf, doc, zip, jpeg, png, gif files allowed" tooltip="Only certain files allowed and filename cannot contain space.Please check and retry"></asp:RegularExpressionValidator>

in client side the validation expression turns to ^[a-za-zA-Z0-9_\.]{3,28}\\.(pdf|txt|doc|docx|png|gif|jpeg|jpg|zip|rar)$ might be because of that

0

精彩评论

暂无评论...
验证码 换一张
取 消