+ 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