0

I've a Laravel 5.8 Application and the application accepts both Hindi & English Text as an Input.

In order to prevent special characters, I've created a Regex Validation which allows English Characters of either case, Numbers, space and '/'.

'title' => 'required|min:3|regex:/^[A-Za-z0-9\-\/ ]+$/',

Furthermore, I have updated it for allowing Hindi Texts.

'title' => 'required|min:3|regex:/^[\u0900-\u097F]*[\s]?([\u0900-\u097F]*[\s]?)*?$/'

This regex /^[\u0900-\u097F]*[\s]?([\u0900-\u097F]*[\s]?)*?$/ does not seem to work. It does not allow Hindi Characters. For Example, I want this string to pass "गुढ़ी पाडवा/चेती चंद / उगाडी / Gudi Padwa/Cheti Chand / Ugadi"

However, this pattern "[A-Za-z0-9\u0900-\u097F\-\/ ]+" works well in html tags, blade file, but not in the controller, when intercepted via Request

Please suggest a valid Regex for allowing Hindi Characters along with English in PHP

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
HV Sharma
  • 4,891
  • 3
  • 15
  • 30
  • 1
    It is not that `^[\u0900-\u097F]*[\s]?([\u0900-\u097F]*[\s]?)*?$` doesn't allow Devanagari characters; it is just that there is no `/` in it. If you already have a regex that works, why this question? – InSync Aug 01 '23 at 15:23
  • It works for client side, in front-end. It does not work in controller in php – HV Sharma Aug 01 '23 at 15:28

1 Answers1

2

PCRE2 doesn't support the \uxxxx syntax by default, according to the docs. Its valid equivalent is \x{xxxx}.

That said, you can use the following:

[A-Za-z0-9\x{0900}-\x{097F}\-\/ ]+

Try it on regex101.com.

Since Devanagari is a recognized script, you can also use the Unicode property syntax: \p{Devanagari}.

[A-Za-z0-9\p{Devanagari}\-\/ ]+

Try it on regex101.com.

InSync
  • 4,851
  • 4
  • 8
  • 30
  • 2
    Note that in PHP, the regex should be used with the `u` modifier (e.g. `/regex/u`) to enable UTF-8 mode. – Olivier Aug 01 '23 at 16:01
  • @InSync It still doesn't work. The dash - in [A-Za-z0-9\x{0900}-\x{097F}\-\/ ]+. ERR: range out of order in character class at offset 20. Escaping the '-' does not match the input. – HV Sharma Aug 04 '23 at 11:01
  • 1
    @HVSharma The regex on its own should match. I'm not entirely sure, but maybe [this](https://stackoverflow.com/q/42577045) is the problem. – InSync Aug 04 '23 at 20:18
  • Olivier s hint and your answer helped. Thanks @InSync. I tried ChatGPT and it's answer "regex:/^[a-zA-Z0-9ऀ-ॿ।॥\/\-\.\s]+$/u" also worked. – HV Sharma Aug 08 '23 at 05:59