简体   繁体   English

如何验证引导表格?

[英]How to validate bootstrap form?

I have made a form using bootstrap (Form component in reactjs) but when I try to click on submit button even if I do not type any input the form gets submitted. 我已经使用bootstrap(reactjs中的Form组件)制作了一个表单,但是当我尝试单击Submit按钮时,即使我没有键入任何输入,表单也会被提交。 How should I validate my form so that when all the input fields are filled then only form is submitted. 我应该如何验证我的表单,以便在填写所有输入字段时仅提交表单。

Form component: 表单组件:

class Form extends Component {

render(){
    return(
      <div>
          <div id="center">
              <form>
                    <div className="form-group">
                         <label htmlFor="firstname">First Name:</label>
                         <input type="firstname" className="form-control" id="firstname" onChange={this.setFirstName}/>
                    </div>

                    <div className="form-group">
                         <label htmlFor="lastname">Last Name:</label>
                         <input type="lastname" className="form-control" id="lastname" onChange={this.setLastName}/>
                    </div>

                    <div className="form-group">
                        <label htmlFor="email">Email address:</label>
                        <input type="email" className="form-control" id="email" onChange={this.setEmailId}/>
                    </div>

                    <div className="form-group">
                         <label htmlFor="bankacc">IBAN:</label>
                         <div id="deletebank" className="items">
                         <input type="bankacc" className="form-control" id="bankacc" onChange={this.setIban}/>
                         <button type="button" className="btn btn-default btn-sm">
                            <span className="glyphicon glyphicon-trash"></span> 
                         </button>
                         </div>
                    </div>

                    <div className="form-group">
                         <label htmlFor="bankname">Bank Name:</label>
                         <input type="bankname" className="form-control" id="bankname" onChange={this.setBankName}/>
                    </div>

                    <div className="form-group">
                         <button type="button" className="btn btn-success" onClick={this.showUser}>Submit</button>
                    </div>


              </form>
          </div>
      </div>

    )}


}

Screenshot: 截图:

在此处输入图片说明

You can make use of html5's require property for simple form validation 

<input type="firstname" 
       className="form-control"
       id="firstname" 
       required
       onChange={this.setFirstName}/>

Also one suggestion is that, I see you are calling different methods for every 
fields. You can simple use 

 onChange={this.handleChange}

  handleChange = (e) => {
        const { value, id } = e.target;
        this.setState({ [id]: e.target.value })
    }

  your state can be 

  state = {
    lastname: '',
    email: '' 
  } etc....

 Your submit button will be 

<button type="submit" className="btn btn-success" onSubmit= . 
{this.handleSubmit}>Submit</button>

 If you want to customize it, you can follow the link.

 https://stackoverflow.com/questions/41296668/reactjs-form-input-validation

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

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