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

115
Visualizações
check decimal format in .tsx

I’m currently looking for a way to check the format of a number.

I want to accept only conventional decimals such as: { 1 ; 1.1 ; 1.01 ; 15 ; 15.15}.

Currently in my text box I can pass numbers like for example: 102.129.392.12 (like an ip adress indeed) etc...

I had thought to put a regex to check that the format is good:

. .match(/^\d+(\.\d{1,2})?$/) 

but I don’t know how to implement it in a .tsx file

I’m new to react's technology.

Here's my code.

const SupplyEvent = () => {

   const handleChangeQuantity = (value: string) => {
       if (chosenSupply) {
          setChosenSupply({
            ...chosenSupply,
            quantity: value.replace(/,/g, '.'),
            // .match(/^\d+(\.\d{1,2})?$/)
          });
       }
  }; 

  return(
     <View style={styles.halfWidth}>
        <Input
           value={chosenSupply?.quantity}
           keyboardType="decimal-pad"
           onChangeText={handleChangeQuantity}
           disabled={mode === FormMode.read}
        />
     </View>
   );
};

I want to add a message in red for the user like 'This number is not accepted'

Do you guys have any ideas ?

Thanks you

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

0

...
import React, { useState } from 'react';
import { Text, StyleSheet } from 'react-native';

const SupplyEvent = () => {
   const [error, setError] = useState(null);

   const handleChangeQuantity = (value: string) => {
       const resolveValue = value.replace(/,/g, '.');

       if(!resolveValue.match(/^\d+(\.\d{1,2})?$/)){
          setError("Format isn't correct");
       }
       else if (chosenSupply) {
          setChosenSupply((prevChosenSupply)=>({
            ...prevChosenSupply,
            quantity: resolveValue
          }));
       }
  }; 

  return(
     <View style={styles.halfWidth}>
        {error && <Text style={styles.errorMessage}>{error}</Text>}
        <Input
           value={chosenSupply?.quantity}
           keyboardType="decimal-pad"
           onChangeText={handleChangeQuantity}
           disabled={mode === FormMode.read}
        />
     </View>
   );
};

const styles = StyleSheet.create({
  ...
  errorMessage: {
    color: 'white',
    backgroundColor: 'red',
    borderRadius: 5
  }
});
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