Skip to content
Advertisement

Laravel validate regex not allow kanji char

I want to validate a form input which not allow input kanji char using Laravel validate rule. How to do it? This is what I did:

public function validationRules()
{
    return [
        ‘name’ => 'regex:/[^x{4e00}-x{9faf}]+/u'
    ];
}

But this work only when all char are kanji. If one of char is not kanji then the regex return valid.

actually now:

ひらがなカタカナ漢字 => valid

123abc => valid

ひらがなカタカナ => valid

漢字 => not valid

what I want:

ひらがなカタカナ漢字 => not valid

123abc => valid

ひらがなカタカナ => valid

漢字 => not valid

Advertisement

Answer

This is final script resolve this case:

function isKatakana($value){
    return preg_match('/[x{3040}-x{309f}x{4e00}-x{9faf}]/u', $value)?false:true;
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement