Home > database >  validation rule to accept html code - Laravel
validation rule to accept html code - Laravel

Time:01-11

<div><img src="https://ci6.googleusercontent.com/proxy/wi-WlYQ2m5IHeGEPm0tMCvanR7D802PRaMo5KxVyrzjaIIzhK61fM3DLzlNomULyXCpB4zsBQQ=s0-d-e1-ft#https://cdn.gmass.co/images/logo.png" alt="ABCD" > <div>Hello JenTenPen,</div><div>You have applied multiple vouchers to your cart.</div><ul><li>New year loyalty applied on cart is going to expire on Monday, 11 October, 2021 at 11:44 PM</li><li>Special promotion applied on Maths Course is going to expire on Monday, 11 October, 2021 at 11:44 PM</li></ul><div>Kindly make your purchase soon to avail these discounts.</div><div>With Warm Regards,</div><div>ABCD</div>

What validation rule can i use to accept only html code like above?

$validator=Validator::make($request->all,[
            'cartTemplate'=>'nullable|string'
        ]);

CodePudding user response:

$regex = "/<(/)?(body|html|head|p|b|strong|a|i|span|div)*>/"

  if (preg_match($regex, $textViewValue) {
     // contains an HTML tag
  } else {
     // doesn't contain an HTML tag
   }

For this problem, I'd be using regular expressions.

CodePudding user response:

There is no specific validation rule in Laravel to check it is valid HTML But Laravel gives the option to add a custom rule.

php artisan make:rule HtmlChecker

In custom rule define the regex for HTML and add it in Laravel Validator

  •  Tags:  
  • Related