react.js验证了两个输入
我目前正在学习React JS。我有一个允许用户注册的寄存器文件。我想在将信息传递给该寄存器文件中的服务器之前,验证两个字段相互匹配。我的代码在下面显示以供视觉视图。我进行了一些搜索,并注意到一个名为“ Handlechange”的基本术语被抛弃了很多,但是我不确定如何将其用于“ Onchange”事件。任何建议将不胜感激。
import React, { useState } from 'react';
import Axios from 'axios';
import './Register.css';
function Register() {
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const register = () => {
Axios.post('http://localhost:3001/user/register', {
firstName: firstName,
lastName: lastName,
email: email,
password: password
}).then((response) => {
console.log(response);
});
};
return (
<div className='Register'>
<h1>Registration</h1>
<div className='RegisterForm'>
<input type='text' name='firstName' placeholder='First Name' onChange={(event) => {setFirstName(event.target.value); }} />
<input type='text' name='lastName' placeholder='Last Name' onChange={(event) => {setLastName(event.target.value); }} />
</div>
<div className='RegisterForm'>
<input type='text' name='email' placeholder='Email'/>
<input type='text' name='emailConfirmation' placeholder='Confirm Email' onChange={(event) => {setEmail(event.target.value); }} />
</div>
<div className='RegisterForm'>
<input type='password' name='password' placeholder='Password'/>
<input type='password' name='passwordConfirmation' placeholder='Confirm Password' onChange={(event) => {setPassword(event.target.value); }} />
</div>
<div className='RegisterForm'>
<button onClick={register}>Register</button>
</div>
</div>
)
}
export default Register;
I am currently in the process of learning React JS. I have a register file that allows the user to register. I want to validate that the two fields match each other before passing the information to the server in that register file. My code is shown below for a visual view. I did some searching around and noticed a fundamental term called "handlechange" was thrown around a lot, but I wasn't sure how to use that with my "onchange" event. Any advice will be appreciated.
import React, { useState } from 'react';
import Axios from 'axios';
import './Register.css';
function Register() {
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const register = () => {
Axios.post('http://localhost:3001/user/register', {
firstName: firstName,
lastName: lastName,
email: email,
password: password
}).then((response) => {
console.log(response);
});
};
return (
<div className='Register'>
<h1>Registration</h1>
<div className='RegisterForm'>
<input type='text' name='firstName' placeholder='First Name' onChange={(event) => {setFirstName(event.target.value); }} />
<input type='text' name='lastName' placeholder='Last Name' onChange={(event) => {setLastName(event.target.value); }} />
</div>
<div className='RegisterForm'>
<input type='text' name='email' placeholder='Email'/>
<input type='text' name='emailConfirmation' placeholder='Confirm Email' onChange={(event) => {setEmail(event.target.value); }} />
</div>
<div className='RegisterForm'>
<input type='password' name='password' placeholder='Password'/>
<input type='password' name='passwordConfirmation' placeholder='Confirm Password' onChange={(event) => {setPassword(event.target.value); }} />
</div>
<div className='RegisterForm'>
<button onClick={register}>Register</button>
</div>
</div>
)
}
export default Register;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要验证字段,则在Axios调用之前,比较所需的字段/值,如果它们匹配,请调用Axios API,否则只需扔出在UI上显示的错误。
If you want to validate fields, then just before the axios call , compare the fields/ values that are required, and if they match then call the axios api else just throw the error that is required to be shown on the UI for user.