How would I go about validating a number in laravel.
I need the number stored in the following format 353861111111.
353 will be the prefix, and the user types in the rest. If the user types in 086, this is invalid.
You can use regex as:
'phone' => 'required|regex:/(353)[0-9]{9}/'
This checks for the pattern with starting with 353 followed by 9 digits having values from 0-9.
Or you can build a custom validator in boot method of AppServiceProvider.php:
Validator::extend('phone_number', function($attribute, $value, $parameters)
{
return substr($value, 0, 3) == '353';
});
This will allow you to use the phone_number validation rule anywhere in your application, so your form validation could be:
'phone' => 'required|numeric|phone_number|size:11'
In your validator extension you could also check if the $value is numeric and 11 characters long.
Here is how I did it on an older Laravel 4 project we have to update from time to time:
'phone' => 'required|regex:/^\d{3}-\d{3}-\d{4}$/',
Try this regex expression:
'number' => 'required|regex:^[3][5][3][\d]{8}[\d]$'
Related
Here is a table named stages and below are the fields
I don't want to allow to add the same from and to points example, if points from 1 to 10 is already added means not allow them to add the same range, another one condition is don't allow to add in between points example in-between 1to 10 like 5 to 7 are also not allowed.
Tried laravel query
$isexist = Stage::whereBetween('from', [$request->from, $request->to])
->orWhereBetweenColumn('to','from', [$request->from, $request->to])->exists();
but it not satisfying all conditions.
Any one please help, thanks in advance.
you can validate the data like this
$validator = Validator::make($inputData, [
'label' => 'required|unique:tableName,label'
]);
if($validator->fails()){
//Throw validation errors
}
Or you can use
Model::query()->updateOrCreate(['label' => $request->label], [
'from' => $request->from,
'to' => $request->to
]);
Read more on unique validation reference: https://laravel.com/docs/9.x/validation#quick-writing-the-validation-logic
I want to validate the form data if it is just numeric/integer (as in just numbers). Based on Laravel's documentation there are two specific validators for that. But the problem I'm facing is that both the validators accept non-numeric characters such as "+" or "-".
numeric
The field under validation must have a numeric value.
integer
The field under validation must be numeric.
How can I make the validation to only accept numbers and not other non-numeric characters?
'main_telephone' => 'numeric',
'main_fax' => 'integer',
'direct_telephone' => 'integer',
'mobile' => 'integer',
Below is the screenshot
if anyone has come across this problem the best possible solution is to use regex. I do not know why I didn't think of this before.
'main_telephone' => 'integer|regex:/^[0-9]*$/',
'main_fax' => 'integer|regex:/^[0-9]*$/',
'direct_telephone' => 'integer|regex:/^[0-9]*$/',
'mobile' => 'integer|regex:/^[0-9]*$/',
I want my input value of range 9-12 and i am applying
'check' => 'required|digits_between:9,12'
digits_between calculate the length of digit.
but i don't want to validate character length. i want to validate only character value
$validatedData = $request->validate([
'check' => 'required|digits_between:9,12',
],
[
'check.digits_between'=> 'Value must be between 9 to 12',
]);
You are looking for the between[min,max] rule:
'integer|between:9,12'
// or
'numeric|between:9,12'
Adding integer or numeric to make sure that between that uses the same system as size treats this as a number.
Laravel 8.x Docs - Validation - Available Rules - between
Laravel 8.x Docs - Validation - Available Rules - integer
Laravel 8.x Docs - Validation - Available Rules - numeric
I want to validate a "price" field in Laravel.
The price should only contain numbers without (.) or (,) and cant start with 0 as well.
Eg
2500 // Pass
02500 // Fails
12.12 // Fails
12,12 / Fails
The rule I have now looks like this:
'price' => 'required|integer|not_in:0',
It seems to work with the example above, however I dont understand it. Why does not integer allow something like 0123 with a starting zero. I just want to make sure that my rule works as excpected and that I dont miss something
Thanks
This works for me:
'price' => 'required|numeric|gt:0',
You can format the input before sending it to the validator in your Request class.
public function formatInput()
{
$input = array_map('trim', $this->all());
$input['price'] = (int)($input['price']);
$this->replace($input);
return $this->all();
}
Then you can use
'price' => 'required|integer|min:0',
Hope this helps
if you're not sure about the rule "integer", you can use the regex expression validation as following :
'price' => 'required|regex:^[1-9][0-9]+|not_in:0',
I had some issue same way as you, and since then i always used the regex validation for this kind of requirements. Furthermore it allow you to take back the same regex if you want to make a front validation with js.
Here is the laravel function allowing to validate integers :
public function validateInteger($attribute, $value)
{
return filter_var($value, FILTER_VALIDATE_INT) !== false;
}
SO it's PHP itself that is concerned by this behavior. PHP tells us this thing about FILTER_VALIDATE_INT :
Validates value as integer, optionally from the specified range, and
converts to int on success.
No other informations are set by PHP about the "0" value, but it's known that this function doesn't consider numbers starting by "0".
Use regex rule to not allow for integer with starting 0
'price' => 'required|integer|not_in:0|regex:^[1-9][0-9]+',
I'm getting Parse error: syntax error, unexpected T_VARIABLE, expecting ')' on the line commented below. Can't for the life of me figure out why it's throwing this error.
public $validate = array(
'password1' => array(
'rule1' => array('rule' => 'alphaNumeric', 'message' => 'Your password should only contain alphanumeric characters.'),
'rule2' => array('rule' => '/\d/', 'message' => 'Your password must contain at least one numeric character.'),
'rule3' => array('rule' => '/^(?=.*?[A-Z])(?=.*?[a-z])/', 'message' => 'Your password must contain at least one uppercase and one lowercase letter.'),
'rule4' => array('rule' => array('minLength', 8), 'message' => 'Your password must be at least 8 characters long.'),
),
'password2' => array(
// ERROR ON LINE BELOW
'rule' => array('_passwordsMatch', $this->data['PasswordReset']['password2']),
'message' => 'The passwords you entered do not match.'
)
);
/**
* Custom validation method to check that the entered passwords match
*
* #param string $password1
* #param string $password2
* #return bool
*/
protected function _passwordsMatch($password1, $password2) {
return ($password1 === $password2);
}
As you can see I'm trying to make a custom validation rule to check the two passwords coming from the user's submitted form. Related question would be is this the wrong way to be trying to pass the other field value to the custom rule?
You are not allowed to reference $this during the initialization syntax of a class property. If you really need that, you must move the array definition to the class constructor.
Quoting the Documentation:
[Properties] are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
This rule is enforced at compile time, so there are grammar rules for static array() syntax that do not allow arbitrary expressions. This is why you get a syntax error: Instead of $this, the parser expects a ) that closes array(.
funny how this comes up this often right now
check out CakePHP validation rule to match field1 and field2 for a clean behavior approach on this subject (see my answer)
also note:
your alphanumeric rule is out of line in my opinion. you should never force a user to use less chars then he wants in a password field. many users use at least some special char and DONT use your upper or lower case style.
i think you are restricting the user more than needed.