我正在開發一個小專案,目前我正在嘗試用php中的JQuery來驗證注冊表單。問題是,我無法為圖片格式找出正確的檢索詞。驗證功能:
function validateRegisterForm(event) {
event.preventDefault()。
var photo=$("#profphoto")[0].files;
var photo_pattern = /. (?:jpeg|jpg|gif)$/
if (!photo_pattern.test(photo)) {
if (isEmpty(照片)) {
$("#error_photo").text("照片不能為空")。
$("#profphoto"/span>).focus()。
return。
}else{
$("#error_photo").text("上傳格式不正確")。
$( "#profphoto" ).focus()。
return。
}
}else {
$("#error_photo"/span>).text(""/span>)。
}
我還有其他的欄位,如姓名、姓等,但它們作業得很好,這就是為什么它們沒有被包括在內。請給我一點幫助!
uj5u.com熱心網友回復:
問題出在測驗函式中使用的變數 "photo",為了解決這個問題,你需要創建一個變數來獲取檔案名,并使用Regex運算式來驗證,例如:
function validateRegisterForm(event){ event.preventDefault()。 var photo=$("#profphoto")[0].files; var filename=photo[0]? .name。 var photo_pattern = /. ( ?:jpeg|jpg|gif)$/ if (!photo_pattern.test(filename)) { if (isEmpty(照片)) { $("#error_photo").text("照片不能為空")。 $("#profphoto"/span>).focus()。 return。 }else{ $("#error_photo").text("上傳格式不正確")。 $( "#profphoto" ).focus()。 return。 } }else { $("#error_photo"/span>).text(""/span>)。 }轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/311777.html
標籤:
