Search code examples
reactjsrequiredrequiredfieldvalidatorrequired-field

ReactJS - How do I validate input fields


I'm using reactstrap components. there is more than one input. how do I set some fields to "required". that is, the user cannot pass idle. how can such restrictions be made?

<Row>
   <Col xs="2">customer name</Col>
   <Col xs="6">
        <Input
        required
        maxLength={"20"}
        type={"text"}
        placeholder={"customer name"}
        name={"customerName"}
        defaultValue={this.state.customerName}
        onChange={this.handleChange}
        />
   </Col>
</Row>

Solution

  • Did you try this one: https://availity.github.io/availity-reactstrap-validation/ ? Does a pretty good job.

    Sorry to write here, but it seems at don't have enough reputation to answer.

    availity-reactstrap-validation extends the reactstrap forms with some nice functions like validation.

    In order to have labels on left and inputs on right you can mix them like this:

    <AvForm onValidSubmit={handleValidSubmit} onInvalidSubmit={handleInvalidSubmit}>
            <FormGroup row>
                <Label for="name" sm={2}>Name</Label>
                <Col sm={10}>
                    <AvField name="name"  type="text" required />
                </Col>
            </FormGroup>
    
        </AvForm>
    

    Be sure that you have imported all the libraries.