Reputation: 99
i tried to apply validation system use jQuery for upload image and multiple images, check file extension and file size but it gives me error uncaught TypeError: Cannot read property 'call' of undefined
hi, i tried to apply validation system use jQuery for upload image and multiple images, check file extension and file size but it gives me error uncaught TypeError: Cannot read property 'call' of undefined
$(document).ready(function () {
jQuery.validator.setDefaults({
debug: true,
success: "valid"
});
$("#myform").validate({
rules: {
"image": {
extension: "jpg|jpeg|png|gif",
filesize: "2048"
},
"images[]": {
extension: "jpg|jpeg|png|gif",
filesize: "2048"
},
}
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>rules</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.validate.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
</head>
<body>
<div class="container">
<form id="myform">
<div class="form-group" id="divim">
<label>image annonce<span class="text-hightlight">*</span></label>
<input type="file" name="image" class="form-control @error('image') is-invalid @enderror" required autocomplete="image">
</div>
<div class="form-group" id="divims">
<label>images annonce<span class="text-hightlight">*</span></label>
<input type="file" name="images[]" class="form-control @error('images') is-invalid @enderror" required autocomplete="images" multiple>
</div>
<button class="btn btn-theme">valider</button>
</form>
</div>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>
Upvotes: 2
Views: 4365
Reputation: 181
Add this code to your additional-methods.js file.
$.validator.addMethod('fileSize', function (value, element, param) {
return this.optional(element) || (element.files[0].size <= param)
}, 'default message');
Upvotes: 2
Reputation: 1257
Running your code, I see the following exception being thrown:
Exception occurred when checking element , check the 'extension' method. TypeError: Cannot read property 'call' of undefined
It seems like the extension method isn't included in the validate plugin by default. It's a separate script you need to include in your html.
Including <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/additional-methods.js"></script>
fixed it for me.
Browsing the documentation of the jQuery validation plugin, I didn't see a method for filesize
. See https://jqueryvalidation.org/jQuery.validator.addMethod/ to create one yourself. Should be straightforward.
For example:
jQuery.validator.addMethod('filesize', function (value, element, arg) {
// value -> name of the file being uploaded
// element.files[0].size -> size of file being uploaded
// arg -> 2048
// Do some validation of filesize and return true or false
});
Upvotes: 2