使用 Jquery、PHP 验证和 Post 文件
Validate and Post file using Jquery, PHP
我正在开发一个小项目,我目前正尝试在 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(photo)) {
$("#error_photo").text("Photo can not be empty");
$("#profphoto").focus();
return;
}else{
$("#error_photo").text("Upload format is not correct");
$( "#profphoto" ).focus();
return;
}
}else {
$("#error_photo").text("");
}
}
I have also other fields like name, lastname ect but they work just fine and thats why they are not included. A little help please!
问题出在测试函数中使用的变量“photo”,要解决您需要创建一个变量来获取文件名并使用正则表达式进行验证,例如:
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(photo)) {
$("#error_photo").text("Photo can not be empty");
$("#profphoto").focus();
return;
}else{
$("#error_photo").text("Upload format is not correct");
$( "#profphoto" ).focus();
return;
}
}else {
$("#error_photo").text("");
}
}
我正在开发一个小项目,我目前正尝试在 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(photo)) {
$("#error_photo").text("Photo can not be empty");
$("#profphoto").focus();
return;
}else{
$("#error_photo").text("Upload format is not correct");
$( "#profphoto" ).focus();
return;
}
}else {
$("#error_photo").text("");
}
}
I have also other fields like name, lastname ect but they work just fine and thats why they are not included. A little help please!
问题出在测试函数中使用的变量“photo”,要解决您需要创建一个变量来获取文件名并使用正则表达式进行验证,例如:
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(photo)) {
$("#error_photo").text("Photo can not be empty");
$("#profphoto").focus();
return;
}else{
$("#error_photo").text("Upload format is not correct");
$( "#profphoto" ).focus();
return;
}
}else {
$("#error_photo").text("");
}
}