I
I
Ily4farmer2021-11-24 00:19:51
Regular Expressions
Ily4farmer, 2021-11-24 00:19:51

Regular expression limiting number to 2 decimal characters?

I am writing a React application, I need a regular expression so that only numbers can be written into the input and after the decimal point you can write a maximum of 2 digits
Here is the code

const FormShopping = observer(() => {
 const [inputText, setInputText] = useState('');

function handleInput(e)

    {   
        let text = e.target.value.replace(/[^\d.]{1, 3}/, '');
        setInputText(text);
    }
})
return (
 <Input onInput={handleInput} value={inputText} type="text" />
)

In regular expressions, I am a complete zero, I tried to write or find something myself, but I did not find a completely correct solution.
Help me please)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel K, 2021-11-24
@PavelK

Greetings.
Well, for example like this:
^([+-]?\d+([\.,]\d{1,2})?)

A
Alexander, 2021-11-24
@zkelo

^(-?\d+(,\d{1,2})?)$

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question