简体   繁体   中英

How to validate “input type file” using jQuery validation engine?

I am using jQuery validation engine to validate a form in my Laravel's project, it works perfect for all of the fields except input type file.

Here is what I have tried:

Form

<form id="series-form" enctype="multipart/form-data" method="post">
    <input type="text" name="series_fname" id="series_fname" class="validate[required]">
    <input type="text" name="series_lname" id="series_lname" class="validate[required]">
    <input type="file" name="series_image" id="series_image" class="validate[required]" value="Upload Image"/>
</form>

jQuery

$(document).ready(function(){
    $("#series-form").validationEngine({promptPosition : "topLeft", 'custom_error_messages': {
       '#series_fname': {
            'required': {
                'message': "* This field is required"
            }
        },
       '#series_lname': {
            'required': {
                'message': "* This field is required"
            }
        },
        '#series_image': {
            'required': {
                'message': "* This field is required"
            }
        },
    });
});

The first two are working fine, but it's not working for image.

It's probably because you already set a value for that field making the validation engine believe it has a value already, even though it's not a file. You should remove the value="Upload Image"

<input type="file" name="series_image" id="series_image" class="validate[required]"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM