Allyssa
Allyssa

Reputation: 63

How do I restrict the input to only number at the input box?

   <Form.Group>
              <Form.Control. onChange={e => { console.log('e------',e.target.value)}}
             type='number' />
   </Form.Group>

I set the type on can be number, but when I click "enter"(return button) and "-",

I still can see "e" and "-" at my input box, but I only want user see number at the inputbox.

Upvotes: 2

Views: 1785

Answers (2)

DevLoverUmar
DevLoverUmar

Reputation: 13933

You can use this

<Form.Control type="number" onKeyDown={ (evt) => evt.key === 'e' && evt.preventDefault() } />

As per an existing answer to a similar question on stackoverflow

Upvotes: 2

Qui-Gon Jinn
Qui-Gon Jinn

Reputation: 4398

You can try regex.

  <Form.Group>
       <Form.Control. 
          onChange={e =>  if(e.target.value.match("/^\d*(\.\d+)?$/")){
              console.log('e------',e.target.value)
          }}
       />
   </Form.Group>
 

Upvotes: 0

Related Questions