Home > OS >  How to give alert message when InputText is empty and submitted?
How to give alert message when InputText is empty and submitted?

Time:02-15

I use react native and TextInput.

And if user submits inputText with empty value, I want to give them a message 'please write a comment'. So I make a register module from useForm with required function.

It seems it prevents empty value created correctly, but alert message is not shown.

      <WritingText
        {...register("comments", { required: "댓글을 적어주세요." })}
        placeholder="댓글을 입력하세요"
        onChangeText={(text) => setValue("comments", text)}
        onSubmitEditing={handleSubmit(onValid)}
      ></WritingText>

CodePudding user response:

For any kind of form validation or input validation, you can use a package called FORMIK - https://formik.org/docs/guides/react-native I hope this will help!

CodePudding user response:

You can do it using formik, if you don't want to use it, you can try the onBlur callback where you can check your text length and then show an alert.

     <WritingText
        {...register("comments", { required: "댓글을 적어주세요." })}
        placeholder="댓글을 입력하세요"
        onChangeText={(text) => setValue("comments", text)}
        onSubmitEditing={handleSubmit(onValid)}
        onBlur={handleBlur}
      ></WritingText>
  • Related