Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactQuill: Validating Empty Input Value (Showing html tags)

Tags:

reactjs

I have problem regarding validating the ReactQuill.

The Scenario: When i remove the input in ReactQuill Textarea the last value will be

<p><br></p>

Question: How to validate if the last value would be <p><br></p> i tried to get value and validate it. but still the value is inserting without data. I don't know where's the problem in my code or in the ReactQuill

Sample Image

State:

 const datas = {
    textValue: this.state.text
 }

My Condition:

 if(datas.textValue.length === 0 || datas.textValue.value == '<p><br></p>')
 {
    return 'false';
 }
 else
 {
    return 'true';
 }
like image 555
DevGe Avatar asked Oct 18 '18 02:10

DevGe


2 Answers

React Quill uses HTML tags for markup purpose. To check length of the entered text by user, we can just filter out html tags from datas.textValue using following regex and trim whitespaces if present.

if(datas.textValue.replace(/<(.|\n)*?>/g, '').trim().length === 0) {
    //textarea is still empty
}
like image 103
Scorpionk Avatar answered Nov 08 '22 07:11

Scorpionk


I had to add just a bit to @Scorpionsk answer for it to work perfectly for me so it wouldn't escape images as well.

This works for me.

function isQuillEmpty(value: string) {
  if (value.replace(/<(.|\n)*?>/g, '').trim().length === 0 && !value.includes("<img")) {
     return true;
   }
     return false;
  }
like image 42
Osei-Bonsu Christian Avatar answered Nov 08 '22 07:11

Osei-Bonsu Christian