如何在React中使用Validator显示无效电子邮件



我正在做一个React应用程序,并安装了用于电子邮件验证的npm install validator,所以一切都很好(如果电子邮件无效,数据不会发送到数据库(,但我不知道当用户输入无效电子邮件时如何显示消息。谢谢大家!

这是我的电子邮件模式:

const validator = require('validator');
const EmailSchema = new mongoose.Schema({
email: {
type: String,
trim: true,
lowercase: true,
unique: true,
validate: {
validator: validator.isEmail,
message: 'Email is invalid',
isAsync: false
}
},
message: {
type: String,
require: true
}
});
const Email = mongoose.model("Email", EmailSchema);
module.exports = Email;

这是我的表格:

<form className="feedback" onSubmit={sendEmail}>
<h5>Leave us a feedback</h5>
<div>
<label>Email:</label>
<input
type="text"
placeholder="Email"
onChange={(event) => {
setEmail(event.target.value);
}}
required
className="input-email"
/>
</div>
<div>
<label>Message:</label>
<input
type="text"
placeholder="Your message here:..."
onChange={(event) => {
setMessage(event.target.value);
}}
required
className="input-food"
/>
</div>
<button className="email-btn">Send Email</button>
</form>
const validator = require('validator');
const EmailSchema = new mongoose.Schema({
email: {
type: String,
trim: true,
lowercase: true,
unique: true,
validate: [ validator.isEmail, 'invalid email' ]
},
message: {
type: String,
require: true
}
});
const Email = mongoose.model("Email", EmailSchema);
module.exports = Email;

相关内容

  • 没有找到相关文章

最新更新