0

I have text input to receive dates, one for Month, another for day, and the third one for the year. I want to check if the entered is valid before sending the data, how could I do that, here is my code

<TextInput
              onChangeText={setMonth}
              value={monthText}
              placeholder="Month"
              returnKeyType={'done'}
              keyboardType={'number-pad'}
              maxLength={2}
              style={{
                backgroundColor: 'white',
                height: 40,
                padding: 10,
                borderRadius: 5,
                borderColor: '#B7B7B7',
                width: 104,
                textAlign: 'center',
              }}
            />

set month is a normal hook

to update it I use

<TouchableOpacity
            style={{
              justifyContent: 'center',
              backgroundColor: '#533AED',
              width: '80%',
              height: 50,
              borderRadius: 50,
              alignItems: 'center',
            }}
            onPress={() => saveUserCardData()}>
            <View
              style={{
                paddingRight: 20,
                alignItems: 'center',
              }}>
              <Text
                style={{
                  fontSize: 16,
                  color: '#FFF',
                  alignItems: 'center',
                  paddingRight: 30,
                  paddingLeft: 40,
                  fontWeight: 'bold',
                }}>
                UPDATE
              </Text>
            </View>
          </TouchableOpacity>

and is runs this function:

const saveUserCardData = () => {
    let data = {
      email: user.email,
      age: dataAge[age].age,
      birthday: `${yearText}-${monthText}-${dayText}`,
      height: heightUnit === 'cm' ? dataHeightCm[height].height : dataHeightFt[height].height, 
      weight: weightUnit === 'lb' ? dataWeightLb[weight].weight : dataWeightKg[weight].weight,
      gender: gender === 'male' ? 'M' : 'F'
    };

where and how should I validate the date?

Thanks for the help.

1 Answers1

0

Maybe momentjs can help in handling date, https://momentjs.com/docs/#/parsing/is-valid/

I would have called a validation function before calling the save function

onPress={() => {
   validateData() ? saveUserCardData() : // handle error
  }
}>
P-A
  • 369
  • 1
  • 8