Reactjs - Form input validation

前端 未结 9 1963
悲哀的现实
悲哀的现实 2020-11-28 20:04

My Contact page form is as follows,

<
9条回答
  •  眼角桃花
    2020-11-28 20:58

    Cleaner way is to use joi-browser package. In the state you should have errors object that includes all the errors in the form. Initially it shoud be set to an empty object. Create schema;

    import Joi from "joi-browser";
    schema = {
        username: Joi.string()
          .required()
          .label("Username")
          .email(),
        password: Joi.string()
          .required()
          .label("Password")
          .min(8)
          .regex(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).{8,1024}$/) //special/number/capital
       };
    

    Then validate the form with the schema:

    validate = () => {
        const options = { abortEarly: false };
        const result = Joi.validate(this.state.data, this.schema, options);
        console.log(data) // always analyze your data
        if (!result.error) return null; 
        const errors = {};
        for (let item of result.error.details) errors[item.path[0]] = item.message; //in details array, there are 2 properties,path and message.path is the name of the input, message is the error message for that input.
        return errors;
      };
    

    Before submitting the form, check the form:

    handleSubmit = e => {
        e.preventDefault();
        const errors = this.validate(); //will return an object
        console.log(errors);
        this.setState({ errors: errors || {} }); //in line 9 if we return {}, we dont need {} here
        if (errors) return;
        //so we dont need to call the server
        alert("success");
        //if there is no error call the server
        this.dosubmit();
      };
    

提交回复
热议问题