【问题标题】:React - adding email validation to empty input validationReact - 将电子邮件验证添加到空输入验证
【发布时间】:2019-11-02 16:55:52
【问题描述】:

在 React 中,我希望将电子邮件验证(检查 @ 和 .com)添加到当前检查空输入字段的表单中。

found this 完成了这项工作,但不知道如何通过我的其他验证将其连接到 onSubmit。

这是完整代码的link to this project's codepen

为输入和错误设置初始状态:

constructor() {
super();
this.state = {
  inputs: {
    name: '',
    email: '',
    message: '',
  },
  errors: {
    name: false,
    email: false,
    message: false,
  },
};

}

JS 处理输入,onBlur

updateInput = e => {
this.setState({
  inputs: {
    ...this.state.inputs,
    [e.target.name]: e.target.value,
  },

  errors: {
    ...this.state.errors,
    [e.target.name]: false,
  },
});
};

handleOnBlur = e => {
const { inputs } = this.state;
if (inputs[e.target.name].length === 0) {
  this.setState({
    errors: {
      ...this.state.errors,
      [e.target.name]: true,
    },
  });
}
};

【问题讨论】:

  • 有什么问题?你有一个有效的代码示例,你为什么不照着做呢?
  • Eric,电子邮件必须以 .com 结尾吗?
  • @ChristopherNgo 理想情况下是的
  • @EricNguyen 请参阅下面的解决方案。如果您有任何问题,请告诉我。 :)
  • HTML5 在你使用 时检查电子邮件

标签: reactjs forms email-validation


【解决方案1】:

一种可能的方法是像这样在代码中添加条件

if((e.target.name === "email") && !this.validateEmail(inputs[e.target.name]) && (inputs[e.target.name].length !== 0 )   ){
     this.setState({
        errors: {
          ...this.state.errors,
          [e.target.name]: true,
        },
      }); 
    }        
 so after generally you will have something like this that add the validate function

validateEmail (email) {
    const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return re.test(email)
  } 

then your  unblur function will look like this

handleOnBlur = e => {
    const { inputs } = this.state;


     if (inputs[e.target.name].length === 0) {
       this.setState({
        errors: {
          ...this.state.errors,
          [e.target.name]: true,
        },
       });
      }
     if((e.target.name === "email") && !this.validateEmail(inputs[e.target.name]) && (inputs[e.target.name].length !== 0 )   ){
      this.setState({
        errors: {
          ...this.state.errors,
          [e.target.name]: true,
        },
      }); 
    }


  };

【讨论】:

    【解决方案2】:

    无需重构太多代码,我们只需将updateInput() 函数更新为:

      updateInput = event => {
        const { name, value } = event.target;
    
        if (name === "email") {
          this.setState({
            inputs: {
              ...this.state.inputs,
              [name]: value
            },
            errors: {
              ...this.state.errors,
              email:
                value.includes("@") &&
                value.slice(-4).includes(".com")
                  ? false
                  : true
            }
          });
        } else {
          this.setState({
            inputs: {
              ...this.state.inputs,
              [name]: value
            },
            errors: {
              ...this.state.errors,
              [name]: false
            }
          });
        }
      };
    

    另见沙盒:https://codesandbox.io/s/conditional-display-input-errors-vfmh5

    【讨论】:

      猜你喜欢
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-02
      • 2018-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多