Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React: Input type number, maxLength not working? [duplicate]

let say I'm trying to get 10 numbers in an Input field but maxLength property didn't work with type='number', although it works fine for type='text'.

import React, { Component } from 'react'
export default props => {
  const {
    handleInput
  } = props;

  return (
    <div>
      <span>+92</span>
      <input type='number' placeholder='Phone Number' maxLength={10} onChange={handleInput}/>
    </div>
  )
}

what im missing in above written code snippet?

One possible work around can be that I use the property of value in input tag and make the type of input as text and by writing an onChange function keep testing input value from Regular Expression.

like image 385
Mubeen Khan Avatar asked Feb 24 '19 01:02

Mubeen Khan


1 Answers

This is a normal HTML element, nothing to do with React.

For input the type number is ignored, you should implement your own validation. According to the Mozilla document: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input#attr-maxlength

like image 191
davychhouk Avatar answered Oct 24 '22 22:10

davychhouk