jQuery文件验证 [英] Jquery file validation

查看:51
本文介绍了jQuery文件验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML表单:

<form id="add">
            <label>Song:</label>
            <input type="text" name="song" class="form-control"><br>
            <label>Upload Mp3:</label>
            <input type="file" name="file" class="form-control"><br>
            <input type="submit" class="btn btn-default" value="Add">
</form>

jQuery验证:

$("#add").validate({
    rules: {
        song: {
            required:true,
            },
           file: { 
                required: false,
                extension: "mp3|mpeg|mp4"
            }, 
        },
    });

extension方法不起作用.每当我提交时,页面都会重新加载.

The extension method isn't working. Whenever I submit, the page reloads.

推荐答案

来自文档

作为附件提供了更多方法,并且当前已包括 在下载包中的Additional-methods.js 中.并非所有人都 记录在这里:

Some more methods are provided as add-ons, and are currently included in additional-methods.js in the download package. Not all of them are documented here:

  • accept –使文件上传仅接受指定的mime类型.
  • 信用卡-使该元素需要一个信用卡号.
  • 扩展名 –使元素需要特定的文件扩展名.
  • accept – Makes a file upload accept only specified mime-types.
  • creditcard – Makes the element require a credit card number.
  • extension – Makes the element require a certain file extension.

additional-methods.js在这里-> https://ajax.aspnetcdn.com/ajax/jquery.validate/1.15.0/additional-methods.js

The additional-methods.js is here -> https://ajax.aspnetcdn.com/ajax/jquery.validate/1.15.0/additional-methods.js

添加后,它就可以正常工作

and after adding it, it works just fine

$("#add").validate({
    rules: {
        song: {
            required: true,
        },
        file: {
            required: false,
            extension: "mp3|mpeg|mp4"
        },
    }
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.15.0/additional-methods.js"></script>

<form id="add" enctype="multipart/form-data">
    <label>Song:</label>
    <input type="text" name="song" class="form-control">
    <br>
    <label>Upload Mp3:</label>
    <input type="file" name="file" class="form-control">
    <br>
    <input type="submit" class="btn btn-default" value="Add">
</form>

这篇关于jQuery文件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆