Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

623
Visualizações
React Mui TextField for currency input - how to permit enter digits only in iPhone Safari browser

I have a Mui TextField component for currency input and I made it to show numeric keyboard only in Safari Browser.

But when the user tries to paste literal string into the field, I'd like to prevent it and make it sure that allows enter currency number inputs only.

import { TextField } from '@mui/material';
export default function CustomCurrencyTestScreen(props){
    const [amount, setAmount] = useState('');

    const handleChange = e => {
       setAmount(e.target.value);
    }
    const handleBlur = e => {
       //some validation functionalities
    }
    
    return (
      <TextField
        size="small"
        id="amount"
        name="amount"
        onChange={handleChange}
        onBlur={handleBlur}
        inputProps={{
           style:{
              fontSize:14,
           }
           inputMode:'numeric',
        }}
        InputProps={{
                startAdornment: (
                    <InputAdornment position="start">
                        $
                    </InputAdornment>
                ),
                endAdornment:(
                  <InputAdornment position='end'>
                    {
                      Boolean(amount) && 
                      
                        <CancelRounded size="small" color="grey" sx={{padding:0}} onClick={()=>
                        setAmount('')}/>
                      
                    }
                  </InputAdornment>
                ),
            }}
      />
    );

}

It shows numeric keyboard successfully in Safari browser, however, when you paste the literal string into the field, the field shows it.

How can I prevent user not to input or paste other characters than numbers(float value also)?

I tried with CurrencyInput and IMaskInput, but there's a problem in implementing startAdornment and endAdornment, so I don't prefer it.

Is there any solution to implement Currency Input with Adornment in Mui, working correctly in Safari Browser?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I don't think there's any way to prevent an actual paste, but you could use a useEffect on amount, and strip out all non-numeric characters:

useEffect(() => {
   function hasNonNumeric(s) {
      // return true if has non numeric
   }

   function stripNonNumeric(s) {
      // logic here that returns a string with non-numeric chars stripped
   }
   if(hasNonNumeric(amount)) {
      setAmount(stripNonNumeric(amount))
   }
}, [amount])

That being said, I probably wouldn't recommend doing it, I hate when websites mess with stuff I paste. Rather, I'd recommend having validation, and showing an error if it contains non-numeric allowing the user to fix it on their terms.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda