Search code examples
laravelvariable-length

Laravel rule to checking length is not equal to N


We have defined some rules in laravel but we need rule in which length of string should not be 5.

code is given below.

$rules = array(
'id' => 'required|numeric|digits_between:7,8'
);

current rule is length would be in between 7 and 8 but i need to modify it to length should be anything but not equal to 5. Please help to resolve.


Solution

  • Never forget regexs :) Here is an example that prevents length =5, not_regex is used cause d{} cant be negated so i consider this the optimal and shortest solution

     'id' =>'required|not_regex:/\b\d{5}\b/',
    

    hope it helps