简体   繁体   English

如何在按钮单击时显示不同的错误消息?

[英]How to show different error message on button click?

Could you please tell me How to show different error message on button click ?你能告诉我如何在按钮点击时显示不同的错误信息吗? Actually when pattern is not matched it should show pattern is not matched .Currently I am only able to show required message .here is my code实际上,当pattern不匹配时,它应该显示pattern is not matched 。目前我只能显示required消息。这是我的代码

 <TextField
          inputRef={register({ required: true, pattern: /^[A-Za-z]+$/i })}
          label="First name"
          variant="outlined"
          name="firstName"
          required
          helperText={errors.firstName && "First name is required"}
          error={errors.firstName ? true : false}
        />

https://codesandbox.io/s/react-hook-form-get-started-j39p0 https://codesandbox.io/s/react-hook-form-get-started-j39p0

在此处输入图片说明

You could use something like react-validation package.你可以使用类似react-validation包的东西。 Using it you will be able to reduce the amount of code you will need to write for your own validation, and also consider the corner cases.使用它,您将能够减少需要为自己的验证编写的代码量,并考虑极端情况。

You define your validation functions at first:首先定义验证函数:

import validator from 'validator';
const required = (value) => {
  if (!value.toString().trim().length) {
    // We can return string or jsx as the 'error' prop for the validated Component
    return 'require';
  }
};

const email = (value) => {
  if (!validator.isEmail(value)) {
    return `${value} is not a valid email.`
  }
};
...

And after that plug them into your form, wherever required:之后,在需要的地方将它们插入到您的表单中:

export default class Login extends Component {
    render() {
        return <Form>
            <h3>Login</h3>
            <div>
                <label>
                    Email*
                    <Input value='email@email.com' name='email' validations={[required, email]}/>
                </label>
            </div>
...

Check out their documentation.查看他们的文档。 It is pretty widely used package and should be easy to implement.它是非常广泛使用的包,应该很容易实现。

Hope this will match with your example Run it here希望这将与您的示例相匹配在此处运行
replace the regular expression with yours用你的替换正则表达式

 import React from "react"; import ReactDOM from "react-dom"; import { useForm } from "react-hook-form"; import { TextField, Button } from "@material-ui/core"; import Autocomplete from "@material-ui/lab/Autocomplete"; import { object, string } from "yup"; class Usernames extends React.Component { constructor(props) { super(props) this.state = { errorText: '', value: props.value } } onChange(event) { if (event.target.value.match(/[abc]/g)) { this.setState({ errorText: 'errr' }) } else { this.setState({ errorText: 'Invalid format' }) } } render() { return ( <TextField hintText="Firstname" floatingLabelText="Firstname" name="name" helperText={this.state.errorText ? this.state.errorText : ""} errorText= {this.state.errorText} onChange={this.onChange.bind(this)} /> ) } } const rootElement = document.getElementById("root"); ReactDOM.render(<Usernames />, rootElement);

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

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