开发者

Adding validation to upload form using jQuery validation plugin

开发者 https://www.devze.com 2023-01-28 02:51 出处:网络
I\'ve got a simple HTML file upload form. I\'m using the jQuery validation plugin in other forms to check for required values and would like to use this in the file upload form to ensure the user has

I've got a simple HTML file upload form. I'm using the jQuery validation plugin in other forms to check for required values and would like to use this in the file upload form to ensure the user has clicked the Browse . . . button first and selected a file. The relevant code is below which is based on how other forms are setup but this one isn't working like the others:

<script src="lib/jquery.js" type="text/javascript" charset="utf-8"></script>
    <script src="lib/jquery.validate.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="lib/jquery.metadata.js" type="text/javascript" charset="utf-8"></script>

    <script type="text/javascript">
    $.metadata.setType("attr", "validate");

    $(document).ready(function(){
    $("#upload").validate();
    });
    </script>

<form enctype="mult开发者_JAVA百科ipart/form-data" action="fileupload.php" method="POST" id='upload'>
Choose a file to upload: <input name="new_file" type="file" validate = "required:true">
<label for = "new_file" class = "error">You must select a file first before you can upload</label>
<input type="submit" name="action" value="Upload">

</form>

Grateful if someone can point out what I'm doing wrong.

Many thanks, Steve


Try by putting your validation script inside document.ready method. i.e.

$(document).ready(function(){
    $.metadata.setType("attr", "validate");

    $(document).ready(function(){
         $("#upload").validate();
    });
});

working demo

0

精彩评论

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