reacttoolbox input component not validating characters

70 Views Asked by At

I am using React-toolbox component Input with type="tel"

How do you prevent the component to not accept invalid characters like alphabets?

I want it to accept only numbers. The input should only be a valid telephone number including international format.


Here is a sample code

<Input type="Type"
    label="Mobile number"
    name="newUserMobileNo"
    value={this.state.newUserMobileNo}
    className="input mobileno"
    onChange={this.newUserMobileNoChanged}
    error={this.state.noNewUserMobileNo}
    theme={theme} />
1

There are 1 best solutions below

2
Smit Vora On

In onChange function (newUserMobileNoChanged) check event.target.value with number regex(/^[0-9\b]+$/),if its valid then setState of "newUserMobileNo" else not.

<Input type="Type"
    label="Mobile number"
    name="newUserMobileNo"
    value={this.state.newUserMobileNo}
    className="input mobileno"
    onChange={this.newUserMobileNoChanged}
    error={this.state.noNewUserMobileNo}
    theme={theme} />