此正则表达式在 Chrome 中不起作用

发布于 2024-10-09 22:58:12 字数 1454 浏览 0 评论 0原文

您好,我刚刚在 jScript 中添加了一个验证函数来验证文件上传控件 [输入类型文件] 中的文件名。该功能似乎在 FF 中运行良好,有时在 ie 中运行良好,但在 Chrome 中则不然。 基本上,该函数测试文件名是否至少为 1 个字符,最多 25 个字符长。仅包含有效字符、数字[无空格]并且属于列表中的文件类型。您能否对此

function validate(Uploadelem) {
    var objRgx = new RegExp(/^[\w]{1,25}\.*\.(jpg|gif|png|jpeg|doc|docx|pdf|txt|rtf)$/);
    objRgx.ignoreCase = true;
    if (objRgx.test(Uploadelem.value)) {
        document.getElementById('moreUploadsLink').style.display = 'block';
    } else {
        document.getElementById('moreUploadsLink').style.display = 'none';
    }
}

编辑 进行一些说明:

不,似乎仍然不起作用,我正在使用 IE 8(尝试了所有兼容模式)、Chrome v8.0、FF v 3.6。 这是一个 html 片段,其中我连接了验证函数,

 <div>
    <input type="file" name="attachment" id="attachment" onchange="validate(this)" />
    <span class="none">Filename should be within (1-25) letters long. Can Contain only letters
        & numbers</span>
    <div id="moreUploads">
    </div>
    <div id="moreUploadsLink" style="display: none;">
        <a href="javascript:addFileInput();">Attach another File</a></div>
</div>

它非常适合我。如何调用验证函数? – M42

您在 Google Chrome 和 IE 8 上尝试过这个吗?我在使用所有推荐的 regX 的地方添加了 HTML 片段。没有任何线索说明为什么不起作用!

迈克,我无法在这里评论你的帖子,所以这是给你的。 我在 html 输入中选择的任何文件的验证都会失败。我还在 onblur 事件中连接了验证,但证明是一样的。验证函数将模仿 asp.net 正则表达式验证器,当不满足正则表达式时显示验证错误消息。

Hi i just put up a validation function in jScript to validate filename in fileupload control[input type file]. The function seems to work fine in FF and sometimes in ie but never in Chrome. Basically the function tests if File name is atleast 1 char upto 25 characters long.Contains only valid characters,numbers [no spaces] and are of file types in the list. Could you throw some light on this

function validate(Uploadelem) {
    var objRgx = new RegExp(/^[\w]{1,25}\.*\.(jpg|gif|png|jpeg|doc|docx|pdf|txt|rtf)$/);
    objRgx.ignoreCase = true;
    if (objRgx.test(Uploadelem.value)) {
        document.getElementById('moreUploadsLink').style.display = 'block';
    } else {
        document.getElementById('moreUploadsLink').style.display = 'none';
    }
}

EDIT:

Nope still does not seem to work , i am using IE 8(tried all the compatibility modes), Chrome v8.0, FF v 3.6.
Here is a html snippet in which i wired up the validate function,

 <div>
    <input type="file" name="attachment" id="attachment" onchange="validate(this)" />
    <span class="none">Filename should be within (1-25) letters long. Can Contain only letters
        & numbers</span>
    <div id="moreUploads">
    </div>
    <div id="moreUploadsLink" style="display: none;">
        <a href="javascript:addFileInput();">Attach another File</a></div>
</div>

It works perfectly for me. How do you call the validate function ? – M42

You tried this on Google Chrome and IE 8 ? i added HTML Snippet in where in i used all of the recommended regX. No Clues as to why doesn't work!!

Mike, i am unable to comment your post here So this is for you.
The Validation Fails for which ever file i choose in the html input. I Also wired the validation in onblur event but proves same. The validate function will mimic the asp.net regular expression validator which displays validation error message when regular expression is not met.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

十二 2024-10-16 22:58:12

尝试简化您的代码。

function validate(Uploadelem) {
    var objRgx = /^[\w]{1,25}\.+(jpg|gif|png|jpeg|doc|docx|pdf|txt|rtf)$/i;
    if (objRgx.test(Uploadelem.value)) {
        document.getElementById('moreUploadsLink').style.display = 'block';
    } else {
        document.getElementById('moreUploadsLink').style.display = 'none';
    }
}

Try simplifying your code.

function validate(Uploadelem) {
    var objRgx = /^[\w]{1,25}\.+(jpg|gif|png|jpeg|doc|docx|pdf|txt|rtf)$/i;
    if (objRgx.test(Uploadelem.value)) {
        document.getElementById('moreUploadsLink').style.display = 'block';
    } else {
        document.getElementById('moreUploadsLink').style.display = 'none';
    }
}
素罗衫 2024-10-16 22:58:12

您的规范很模糊,但您似乎希望在文件名中允许使用点(除了分隔文件名和扩展名的点之外)。

在这种情况下,请尝试

var objRbx = /^[\w.]{1,25}\.(jpg|gif|png|jpeg|doc|docx|pdf|txt|rtf)$/i;

这允许文件名仅包含字符 azAZ0-9_.,后跟必需的点和指定的扩展名之一。

Your specification is hazy, but it appears that you want to allow dots within filenames (in addition to the dot that separates filename and extension).

In that case, try

var objRbx = /^[\w.]{1,25}\.(jpg|gif|png|jpeg|doc|docx|pdf|txt|rtf)$/i;

This allows filenames that consist only of the characters a-z, A-Z, 0-9, _ and ., followed by a required dot and one of the specified extensions.

策马西风 2024-10-16 22:58:12

据我所知,Chrome 在输入的文件名前面添加了一个路径,因此您只需将正则表达式从: 更改

/^[\w]{1,25}\.*\.(jpg|gif|png|jpeg|doc|docx|pdf|txt|rtf)$/

为:

/\b[\w]{1,25}\.+(jpg|gif|png|jpeg|doc|docx|pdf|txt|rtf)$/

As far as I know, Chrome adds a path in front of the filename entered, so you have just to change your regex from:

/^[\w]{1,25}\.*\.(jpg|gif|png|jpeg|doc|docx|pdf|txt|rtf)$/

to:

/\b[\w]{1,25}\.+(jpg|gif|png|jpeg|doc|docx|pdf|txt|rtf)$/
╰◇生如夏花灿烂 2024-10-16 22:58:12

已解决

所有 [正确的 regx 模式] 不起作用的主要原因是不同的浏览器为 HTML 文件输入控件返回了不同的值。

Firefox:返回文件上传控件 FileName {按预期}

Internet Explorer:返回从驱动器到文件的完整路径 [痛苦]

Chrome:返回虚假路径 [C:\FakePath\Filename.extension]

I找到了适用于 chrome 和 FF 的解决方案,但没有解决 IE 的问题。

Chrome 和 Firefox:
使用 FileUploadControlID.files[0].fileName 或 FileUploadControlID.files[0].name

IE
再次最大的痛苦是[有人提出了一个解决方案]

验证文件名和扩展名的有效正则表达式是:

/\b([a-zA-Z0-9._/s]{3,50})(?=(\.((jpg)|(gif)|(jpeg)|(png))$))/i

1.文件名应该在 3 到 50 个字符之间
2. 只允许 jpg,gif,jpeg,png 文件

SOLVED

Primary reason that all [CORRECT regx pattern] did not work is Different browsers returned different values for HTML File Input control.

Firefox: Returns the File Upload controls FileName {As Expected}

Internet Explorer: Returns the Full Path to the File from Drive to File [Pain in the Ass]

Chrome: Returns a fake path as [C:\FakePath\Filename.extension]

I got a solution to the thing for chrome and FF but not IE.

Chrome and Firefox:
use FileUploadControlID.files[0].fileName or FileUploadControlID.files[0].name

IE
Again biggest pain in the ass [someone suggest a solution]

Valid Regex to Validate both fileName and Extension would be:

/\b([a-zA-Z0-9._/s]{3,50})(?=(\.((jpg)|(gif)|(jpeg)|(png))$))/i

1.File Nameshould be between 3 and 50 characters
2. Only jpg,gif,jpeg,png files are allowed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文