Hyejung
Hyejung

Reputation: 1272

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

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>

Upvotes: 1

Views: 283

Answers (1)

Sourav Dey
Sourav Dey

Reputation: 1150

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>

Upvotes: 1

Related Questions