function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SS KarthickSS Karthick 

validation in rich text area data type

Hi folks
           Can anyone tell me how to validate if the rich text area has image and html tag


Thanks in advance
Karthick
Best Answer chosen by SS Karthick
Vikash TiwaryVikash Tiwary
Hi,

You can form a string within while loop which contains comma seperated HTML tags and then you can check if that string contains img tag as below:

Intranet_News__c obj = [select Story__c from Intranet_News__c where Id = 'a2Nc0000000DL3H'];

string strRichtext = obj.Story__c;
string strTags = '';
while(strRichtext.indexOf('<') > -1 && strRichtext.indexOf('>') > -1)
{
   Integer i =  strRichtext.indexOf('<');
   Integer j = strRichtext.indexOf('>');
  string strtobeReplaced = strRichtext.substring(i, j+1);
 strTags = (strTags == '') ? strtobeReplaced : strTags + ', '+strtobeReplaced;
  strRichtext = strRichtext.replace(strtobeReplaced, '');

}
if(strTags.indexOf('<img>') > -1)
//This means the Rich text field contains image.

Thanks

All Answers

Vikash TiwaryVikash Tiwary
Hi,

You can form a string within while loop which contains comma seperated HTML tags and then you can check if that string contains img tag as below:

Intranet_News__c obj = [select Story__c from Intranet_News__c where Id = 'a2Nc0000000DL3H'];

string strRichtext = obj.Story__c;
string strTags = '';
while(strRichtext.indexOf('<') > -1 && strRichtext.indexOf('>') > -1)
{
   Integer i =  strRichtext.indexOf('<');
   Integer j = strRichtext.indexOf('>');
  string strtobeReplaced = strRichtext.substring(i, j+1);
 strTags = (strTags == '') ? strtobeReplaced : strTags + ', '+strtobeReplaced;
  strRichtext = strRichtext.replace(strtobeReplaced, '');

}
if(strTags.indexOf('<img>') > -1)
//This means the Rich text field contains image.

Thanks
This was selected as the best answer
Vikash TiwaryVikash Tiwary
Okay..Please mark the above answer if it resolves your query.
Thanks