react.js验证了两个输入

发布于 2025-01-27 16:05:26 字数 1892 浏览 2 评论 0原文

我目前正在学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桜花祭 2025-02-03 16:05:26

如果要验证字段,则在Axios调用之前,比较所需的字段/值,如果它们匹配,请调用Axios API,否则只需扔出在UI上显示的错误。

//Add validation inside the function
 const register = () => { 
    // check if firstName and LastName is not empty 
    // or any other validation you want - add it here.
      if(firstName === "" || LastName ===""){ 
           // alert the user for invalid values
            alert(" FirstName and LastName cannot be empty");
       }
       else  {
       Axios.post('http://localhost:3001/user/register', {
       firstName: firstName,
       lastName: lastName,
       email: email,
       password: password
     }).then((response) => {
       console.log(response);
     });
    }

   };


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.

//Add validation inside the function
 const register = () => { 
    // check if firstName and LastName is not empty 
    // or any other validation you want - add it here.
      if(firstName === "" || LastName ===""){ 
           // alert the user for invalid values
            alert(" FirstName and LastName cannot be empty");
       }
       else  {
       Axios.post('http://localhost:3001/user/register', {
       firstName: firstName,
       lastName: lastName,
       email: email,
       password: password
     }).then((response) => {
       console.log(response);
     });
    }

   };


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文