在 React JS 中进行表单验证后,如何让我的错误消息消失?
How do I get my error message to go away after form validation in react js?
我有一个简单的登录表单,它接收用户电子邮件和用户密码,目的是在输入字段下方直接弹出一条错误消息,并在输入框留空时在输入框周围添加一个红色边框或者模式是错误的。只有我的输入字段之一运行正常。 (输入字段似乎相互依赖?)任何帮助将不胜感激!
一旦密码符合要求,另一个输入字段就会删除红色边框,但是错误消息仍然存在。目标是同时删除红色边框和错误消息
class Login extends Component {
constructor(props) {
super(props);
this.emailInput = React.createRef();
this.passwordInput = React.createRef();
this.state = {
email: "",
password: "",
emailError: false,
passwordError: false,
};
this.handleSubmit = this.handleSubmit.bind(this);
this.handleInputChange = this.handleInputChange.bind(this);
this.validate = this.validate.bind(this);
}
handleInputChange(e) {
const { value, name } = e.target;
this.setState({ [name]: value });
console.log(value);
}
validateEmail(userEmail) {
const emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
return emailPattern.test(userEmail);
}
validatePassword(userPassword) {
const passwordPattern =
/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])(?=.{8,})/;
return passwordPattern.test(userPassword);
}
styleBorderEmail(emailInput, styles) {
this.emailInput.current.style.border = styles;
}
styleBorderPassword(passwordInput, styles) {
this.passwordInput.current.style.border = styles;
}
validate() {
let emailError = this.email;
let passwordError = this.password;
if (!this.validateEmail(this.state.email)) {
emailError = "email not valid";
this.styleBorderEmail(this.emailInput, "1px solid red");
} else {
this.styleBorderEmail(this.emailInput, "1px solid black");
}
if (!this.validatePassword(this.state.password)) {
passwordError = "password not valid";
this.styleBorderPassword(this.passwordInput, "1px solid red");
} else {
this.styleBorderPassword(this.passwordInput, "1px solid black");
}
if (emailError || passwordError) {
this.setState({ emailError, passwordError });
return false;
}
return true;
}
handleSubmit(e) {
e.preventDefault();
const isValid = this.validate();
if (isValid) {
alert("successful submit");
// clears the form after it's been submitted
this.setState({
email: "",
password: "",
emailError: false,
passwordError: false,
});
}
}
render() {
return (
<div className="login-component">
<div className="login-container">
<h2>Welcome Back!</h2>
<br />
<form onSubmit={this.handleSubmit}>
<div className="login-group">
<label htmlFor="email" className="login-label">
Your email
</label>
<input
className="login-input"
type="text"
name="email"
id="email"
placeholder="Enter your email"
value={this.state.email}
onChange={this.handleInputChange}
onKeyUp={this.validate}
ref={this.emailInput}
/>
{this.state.emailError && (
<span className="error-login">
Please enter a valid email address
</span>
)}
</div>
<div className="login-group">
<label htmlFor="password" className="login-label">
Password
</label>
<input
className="login-input"
type="password"
name="password"
id="password"
placeholder="Enter your password"
value={this.state.password}
onChange={this.handleInputChange}
onKeyUp={this.validate}
ref={this.passwordInput}
/>
{this.state.passwordError && (
<span className="error-login">Please enter a password</span>
)}
</div>
<div>
<button type="submit" className="login-btn">
Login
</button>
</div>
</form>
</div>
</div>
);
}
}
export default Login;
你需要在验证函数中添加一行代码this.setState({ emailError: "", passwordError: "" });
然后返回true。
新的验证函数如下所示
validate() {
let emailError = this.email;
let passwordError = this.password;
if (!this.validateEmail(this.state.email)) {
emailError = "email not valid";
this.styleBorderEmail(this.emailInput, "1px solid red");
} else {
this.styleBorderEmail(this.emailInput, "1px solid black");
}
if (!this.validatePassword(this.state.password)) {
passwordError = "password not valid";
this.styleBorderPassword(this.passwordInput, "1px solid red");
} else {
this.styleBorderPassword(this.passwordInput, "1px solid black");
}
if (emailError || passwordError) {
this.setState({ emailError, passwordError });
return false;
}
this.setState({ emailError: "", passwordError: "" }); // New line added
return true;
}
我有一个简单的登录表单,它接收用户电子邮件和用户密码,目的是在输入字段下方直接弹出一条错误消息,并在输入框留空时在输入框周围添加一个红色边框或者模式是错误的。只有我的输入字段之一运行正常。 (输入字段似乎相互依赖?)任何帮助将不胜感激!
一旦密码符合要求,另一个输入字段就会删除红色边框,但是错误消息仍然存在。目标是同时删除红色边框和错误消息
class Login extends Component {
constructor(props) {
super(props);
this.emailInput = React.createRef();
this.passwordInput = React.createRef();
this.state = {
email: "",
password: "",
emailError: false,
passwordError: false,
};
this.handleSubmit = this.handleSubmit.bind(this);
this.handleInputChange = this.handleInputChange.bind(this);
this.validate = this.validate.bind(this);
}
handleInputChange(e) {
const { value, name } = e.target;
this.setState({ [name]: value });
console.log(value);
}
validateEmail(userEmail) {
const emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
return emailPattern.test(userEmail);
}
validatePassword(userPassword) {
const passwordPattern =
/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])(?=.{8,})/;
return passwordPattern.test(userPassword);
}
styleBorderEmail(emailInput, styles) {
this.emailInput.current.style.border = styles;
}
styleBorderPassword(passwordInput, styles) {
this.passwordInput.current.style.border = styles;
}
validate() {
let emailError = this.email;
let passwordError = this.password;
if (!this.validateEmail(this.state.email)) {
emailError = "email not valid";
this.styleBorderEmail(this.emailInput, "1px solid red");
} else {
this.styleBorderEmail(this.emailInput, "1px solid black");
}
if (!this.validatePassword(this.state.password)) {
passwordError = "password not valid";
this.styleBorderPassword(this.passwordInput, "1px solid red");
} else {
this.styleBorderPassword(this.passwordInput, "1px solid black");
}
if (emailError || passwordError) {
this.setState({ emailError, passwordError });
return false;
}
return true;
}
handleSubmit(e) {
e.preventDefault();
const isValid = this.validate();
if (isValid) {
alert("successful submit");
// clears the form after it's been submitted
this.setState({
email: "",
password: "",
emailError: false,
passwordError: false,
});
}
}
render() {
return (
<div className="login-component">
<div className="login-container">
<h2>Welcome Back!</h2>
<br />
<form onSubmit={this.handleSubmit}>
<div className="login-group">
<label htmlFor="email" className="login-label">
Your email
</label>
<input
className="login-input"
type="text"
name="email"
id="email"
placeholder="Enter your email"
value={this.state.email}
onChange={this.handleInputChange}
onKeyUp={this.validate}
ref={this.emailInput}
/>
{this.state.emailError && (
<span className="error-login">
Please enter a valid email address
</span>
)}
</div>
<div className="login-group">
<label htmlFor="password" className="login-label">
Password
</label>
<input
className="login-input"
type="password"
name="password"
id="password"
placeholder="Enter your password"
value={this.state.password}
onChange={this.handleInputChange}
onKeyUp={this.validate}
ref={this.passwordInput}
/>
{this.state.passwordError && (
<span className="error-login">Please enter a password</span>
)}
</div>
<div>
<button type="submit" className="login-btn">
Login
</button>
</div>
</form>
</div>
</div>
);
}
}
export default Login;
你需要在验证函数中添加一行代码this.setState({ emailError: "", passwordError: "" });
然后返回true。
新的验证函数如下所示
validate() {
let emailError = this.email;
let passwordError = this.password;
if (!this.validateEmail(this.state.email)) {
emailError = "email not valid";
this.styleBorderEmail(this.emailInput, "1px solid red");
} else {
this.styleBorderEmail(this.emailInput, "1px solid black");
}
if (!this.validatePassword(this.state.password)) {
passwordError = "password not valid";
this.styleBorderPassword(this.passwordInput, "1px solid red");
} else {
this.styleBorderPassword(this.passwordInput, "1px solid black");
}
if (emailError || passwordError) {
this.setState({ emailError, passwordError });
return false;
}
this.setState({ emailError: "", passwordError: "" }); // New line added
return true;
}