如何更改 jquery 驗證的默認錯誤訊息?
這是我的html表單:
<form id="form_detail_file3" action="{$form_action}" method="POST" enctype='multipart/form-data'>
<div class="mb-3">
<label for="file" class="form-label">Inserisci il documento:</label>
<input class="form-control" type="file" accept="image/png, image/gif, image/jpeg" name="fileToUpload"
id="fileToUpload">
</div>
</form>
和 JS:
$("#form_detail_file3").validate({
rules: {
fileToUpload: {
required: true,
extension: "jpg|jpeg|png|ico|bmp"
}
},
messages: {
fileToUpload: {
required: "Please upload file.",
extension: "Please upload file in these format only (jpg, jpeg, png, ico, bmp)."
}
}
});
該代碼正在運行,當沒有要上傳的檔案時,我會收到 require 訊息,但是如果提供的擴展名未匹配,則會顯示此默認訊息“請輸入具有有效 mimetype 的值。”。
uj5u.com熱心網友回復:
如果擴展名不匹配,則顯示“請輸入具有有效 mimetype 的值。 ”
Mimetype 與檔案擴展名無關,因此這不能是extension規則的默認訊息。
"enter valid mimetype"是來自規則的默認錯誤訊息accept,它驗證 mimetype,并且您已在此處宣告與 HTML 標記行內:
<input class="form-control" type="file" accept="image/png, image/gif, image/jpeg" name="fileToUpload" ....
當您使用一種一致的方法宣告所有規則時,此插件更易于使用和理解,而不是將行內 HTML5 規則與您的 jQuery 混合.validate() rules。
jQuery Validate 插件自動將各種行內宣告轉換為規則,例如 HTML5 和某些類名。
示例:行內required="required"or將執行與 的required: trueinsiderules選項相同的 操作.validate()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/442736.html
上一篇:在Dropdown中動態添加選項
