开发者

Debugging RegularExpressionValidator control

开发者 https://www.devze.com 2023-01-30 21:19 出处:网络
I have an asp:regularexpressionvalidator with the following expression to validate a file upload. \\.{1}x?html?$

I have an asp:regularexpressionvalidator with the following expression to validate a file upload.

\.{1}x?html?$

Expresso shows me that it works. The simple html test page below shows that it works, but in my application, loading an html file in the file input always triggers the validator?!?

Anyone have any ideas or tips on debugging the javascript of this control?

Thanks, Sam

the control:

<asp:RegularExpressionValidator ID="validRegexHtmlOnly" runat="server" ControlToValidate="fileImportHTML" 
        ValidationExpression="\.{1}x?html?" ErrorMessage="The file must be an HTML document (.html, .htm, .xhtml)" Display="Dynamic" />

simple test page:

<html>
<head>
<script type="text/javascript">
function validate(){
 var txt = document.getElementById("txtInput").value;
 alert(/\.{1}x?html?$/.test(txt));
}
</script>
</head>
<body>
<input type="file" id="txtInput" />
<input type="button" onclick="validate()" value="cli开发者_StackOverflowck me" />
</body>
</html>


I believe the regex in a RegularExpressionValidator is supposed to match the whole string, not just the end (or the beginning, or whatever). Try changing your regex to

.*\.x?html?

On a side note, the {1} wasn't doing anything useful, so I removed it. It's not necessary to tell a regex to match one of something; that's the default behavior. And {1} won't stop it from matching more than one of the thing it's attached to, either. All it does is clutter up your regex.


My best guess is that you are getting whitespace on the end of the filename. Not quite sure how that would happen in a web page, but it's the only thing I can think of.

0

精彩评论

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