Validate a numeric string has a fixed amount of chars - laravel

I'm trying to use a validator to validate that the field cedula is a numeric string but also have 10 chars.
For example:
132456 Would not be validated because it is a numeric string but only has 6 chars
1315987423 Would be validated because it is a numeric string and has 10 chars
131598742B Would not be validated because it isn't a numeric string because of the B in the end
Im trying to validate it like this in the validator
'cedula' => ['required', 'numeric', 'size:10'],
But it only allows me to input the value of 10 and not a numeric string of 10 chars like 1315987423 that is what I desire to validate
How can I make the validator do what I need to do in this case?
Cedula is a string, not an int
Edit: Already realized that I should use digits to validate what I want to do.

I have found this post about your question.
I supose that this is the info which you are loocking for.
'cedula' => 'numeric|required|unique:personas|min:100000|max:99999999',

This should work
'cedula' => ['required', 'digits:10'],
The integer under validation must have an exact length of value.
Source: https://laravel.com/docs/9.x/validation#rule-digits

Related

Convert string to number format in Laravel

I'm trying to convert a string number like 14,767 or 96,812.10 or 6,780,766.50 but if I use
(double)$fildOfDataBase or (float)$fildOfDataBase this put a integer format as 14 o 96 and I don't know why, so my question is how can i convert a string to a number format, hope some one can help me I'm using laravel 8
You need to remove the thousands separator first
(float)str_replace(',', '', $fildOfDataBase);
Another more "strict" way to do it is to use numfmt_parse
//in your case use the en_EN format
$fmt = numfmt_create( 'en_EN', NumberFormatter::DECIMAL );
numfmt_parse($fmt, $fildOfDataBase);

HEXADECIMAL string to Binary and Binary to decimal in log-stash using ruby filter

I am working with logstash and I am using ruby filter to change HEX to decimal which works fine for one set of data. What I use for this is ;
ruby{
code => "event.set('xxx', event.get('xx').hex)"
}
I am looking for something similar that will convert HEX to binary then also convert it to decimal after I grok against patterm.The reason I need to do this is because I am picking uneven bits from the hex values e.g
Hex string - "ABC"
BINARY - "101010111100"
Then I will use GROK to only pick the first three bits "101" and convert to decimal field which is the value I want , and also for example the next "010" to another field. I am just after help with the two conversions.Not Grok
You can do the conversions by using to_i and to_s and specifying the base
mutate { add_field => { "[HexNum]" => "4a" } }
ruby {
code => '
hexNum = event.get("HexNum")
event.set("binNum", hexNum.to_i(16).to_s(2))
'
}
You can then use your grok filter to pick out the parts of [binNum] that you want and put them into a couple of fields, then convert them to decimal
ruby {
code => '
event.set("field1", event.get("field1").to_i(2).to_s)
event.set("field2", event.get("field2").to_i(2).to_s)
'
}

How to format to decimal number that include comma and dot in laravel?

I would like to know that how can i format my number including comma and dot to decimal. Thanks. In my database , my amount column is decimal type with (16,8) . after i formatting i want to save to database my number . For example
my input number 10,000.1111 and want to save as a 10000.1111 to my database. Thanks.
You could use this to format the string to a float, and remove the commas.
(float) str_replace(',', '', '10,000.1111')
Typecasting (float) will force the outputted variable as a float. And str_replace() will remove the commas from the inputted variable.
$input = '10,000.1111';
$model->column = (float) str_replace(',', '', $input);
$model->save()

validation on exact length of numeric field in laravel 5

I have numeric field i want to validate on exact fill 4 digits.
like: 1234, 4567,1245
but these invalid inputs:
like: 123, 346, 45m6, 34567
what I am trying:
'last4digits' => 'numeric|between:4,4',
You are looking for the digits rule. From the docs:
The field under validation must be numeric and must have an exact length of value.
'last4digits' => 'digits:4'
in general case ,say, for a string with arbitrary characters:
'last_not_only_4_digits' => 'size:4'

Is it possible to identify the format of a string?

Is it possible to recognize if a string is formatted as a BSON ObjectID?
For strings we could do:
"hello".is_a?(String) # => true
That would not work since the ObjectID is a String anyway. But is it possible to analyze the string to determine if it's formatted as a BSON ObjectID?
Usually, ObjectIDs have this format.
52f4e2274d6f6865080c0000
The formatting criteria is stated in the docs:
ObjectId is a 12-byte BSON type, constructed using:
a 4-byte value representing the seconds since the Unix epoch,
a 3-byte machine identifier,
a 2-byte process id, and
a 3-byte counter, starting with a random value.
Any 24 chararcters long hexadecimal string is a valid BSON object id, so you can check it using this regular expression:
'52f4e2274d6f6865080c0000' =~ /\A\h{24}\z/
# => 0
Both the moped (used by mongoid) and the bson (used by mongo_mapper) gems encapsulates this check in a legal? method:
require 'moped'
Moped::BSON::ObjectId.legal?('00' * 12)
# => true
require 'bson'
BSON::ObjectId.legal?('00' * 12)
# => true
In Mongoid use: .is_a?(Moped::BSON::ObjectId) sytanx.
Example:
some_id = YourModel.first.id
some_id.is_a?(Moped::BSON::ObjectId)
Note:
"52d7874679478f45e8000001".is_a?(String) # Prints true

Resources