简体   繁体   English

如何根据 React Material Ui 中其他文本字段的输入来验证文本字段?

[英]How to validate textfield based on input of other textfield in React Material Ui?

I am using material ui textfield.我正在使用材料 ui 文本字段。 I ran into problem where I Want to write a code where one textfield is dependent on other.我遇到了一个问题,我想编写一个文本字段依赖于另一个文本字段的代码。 Like if in textfield one i enter no 4, then number in textfield two should always be greater than 4.That means textfield two depends on textfield one.就像如果在文本字段一中我输入没有 4,那么文本字段二中的数字应该始终大于 4。这意味着文本字段二取决于文本字段一。 Please see code in code sandbox.请参阅代码沙箱中的代码。 https://codesandbox.io/s/textfield-with-outline-color-forked-8oj2z https://codesandbox.io/s/textfield-with-outline-color-forked-8oj2z

       <TextField
          id="outlined-email-input"
          label="Email"
          className={classes.textField}
          type="number"
          defaultValue="0"
          name="email"
          autoComplete="email"
          margin="normal"
          variant="outlined"
        />

You can declare the states for storing the values of text1 and text2, and compare them for validation.您可以声明用于存储 text1 和 text2 的值的状态,并比较它们以进行验证。

Following is the example along with codesandbox link.以下是示例以及代码和框链接。

Output: Output:

在此处输入图像描述



class OutlinedTextFields extends React.Component {
  state = {
    txt1: 0,
    txt2: 0
  };


  render() {
    const { classes } = this.props;

    return (
      <form className={classes.container} noValidate autoComplete="off">
        <TextField
          id="outlined-email-input"
          label="Text1"
          className={classes.textField}
          type="number"
          defaultValue={this.state.txt1}
          onChange={(event) => {
            this.setState({
              txt1: parseInt(event.target.value)
            });
          }}
          margin="normal"
          variant="outlined"
        />
        <TextField
          id="outlined-password-input"
          label={
            this.state.txt1 >= this.state.txt2
              ? "Text2 should be greater than Text1"
              : "Text2"
          }
          className={classes.textField}
          type="number"
          error={this.state.txt1 >= this.state.txt2}
          defaultValue={this.state.txt2}
          onChange={(event) => {
            this.setState({
              txt2: parseInt(event.target.value)
            });
          }}
          name="password"
          margin="normal"
          variant="outlined"
        />
        <Button
          margin="normal"
          variant="outlined"
          disabled={
            this.state.txt1 >= this.state.txt2 ||
            !this.state.txt1 ||
            !this.state.txt2
          }
        >
          Submit
        </Button>
      </form>
    );
  }
}

Codesandbox Link 代码沙盒链接

one of the possible way to address this: Btw: it's just a draft with an idea, u can improve it as much as posible.解决这个问题的一种可能方法:顺便说一句:这只是一个有想法的草稿,你可以尽可能地改进它。

const OutlinedTextFields = () => {
  const [inpValue, setInputValue] = useState();
  const handleInputVal = (val) => {
    if (!val) {
      return 0;
    }
    if (Number(val) !== 4) {
      return Number(val) + 1;
    } else {
      return val;
    }
  };
  return (
    <form noValidate autoComplete="off">
      <TextField
        id="outlined-email-input"
        label="Email"
        type="number"
        defaultValue="0"
        name="email"
        autoComplete="email"
        margin="normal"
        variant="outlined"
        onChange={(e) => setInputValue(e.target.value)}
      />
      <TextField
        id="outlined-password-input"
        label="Password"
        type="number"
        defaultValue="0"
        name="password"
        autoComplete="current-password"
        margin="normal"
        variant="outlined"
        value={handleInputVal(inpValue)}
      />
    </form>
  );
};

Material UI Textfields have an error prop you can use. Material UI Textfields 有一个你可以使用的错误道具。 Than declare the error in your state as false.然后将 state 中的错误声明为 false。 In your onChange function add a condition.在您的 onChange function 添加条件。

 const OutlinedTextFields = () => { const [inputValue, setInputValue] = useState(''); const [error, setError] = useState(false) const handleInput = (val) => { if (;val) { return 0; } if (val <= inputValue) { return setError(true); } else { setError(false); return val; } }. return ( <form noValidate autoComplete="off"> <TextField... onChange={(e) => setInputValue(e.target.value)} /> <TextField... error={error} helperText={error && `Number must be greater than ${inputValue}`} onChange={(e) => handleInput(e.target;value)} /> </form> ); };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM