How can write regex validate for zipcode in laravel validation? - laravel

I have a registration form. I need to validate zipcode. Validation rules must be integer, Max length not greater than 7.
I had tried validation but it validate zip must be 5. But some countries has 6 or 4 zipcode. Any help would be appreciated.
'zipcode' => 'required|regex:/^[0-9]{5}(\-[0-9]{4})?$/',

If what you want is numeric digits only, at least 3 but no more than 7, you should use
'zipcode' => 'required|regex:/^[0-9]{3,7}$/'

Related

Laravel validation for two column value

I have one table which contains two column field, 1.frame_id 2.extra_frame_id
Now I want to check if this two column contains vice-versa value or not by laravel validation.
Let say frame_id contains id = 1, where extra_frame_id contains = 2,5,7
Now If I'm adding value frame_id = 5 and extra_frame_id 1,4 then it should fire an error.
Please let me know if it's achievable via laravel validation

Laravel Eloquent date_format Validation fails on given format

I want to create validation rule to validate incoming date.
Format I want to validate is Y-m-d H:i:s. Here's my body request which I am validating:
{ "date":"2015.10.5 10:30:10" }
And here's my validation rule:
'date' => 'required|date_format:"Y.m.d H:i:s"',
And it returns:
{"date":["The date does not match the format Y.m.d H:i:s."]}
If you want to be able to pass the day without a leading zero, then for the day part of your datetime you need to use j instead of d.
'date' => 'required|date_format:"Y.m.j H:i:s"',
That will work for the example you have above.
If you are going to always have a leading zero in your day (so, 05 instead of just 5, then the date format you already have will work.

How can we format a currency using culture group size in Kendo numeric text box?

I am using Kendo numericTextBox to display the currency. I have a requirement to format the value based on the currency selected. I am able to format the currency correctly for "en-US" and "de-DE" but I'm having trouble to format the currency correctly for culture have different group size.
All the example and sample in Kendo blog are on "en-US" and "de-DE" which have similar group size.
For "en-US" currency groupSize property is [3] what means that each group will be separated after 3 digits e.g. 1,000,000. But for some other culture which have different grouping, e.g. "en-IN" which have the 'groupSize' equals [3,2,0], kendo still group the number in group of 3 only: 1,000,000, while we expect the grouping to be 3 digits then separator then group of 2 digits etc.: 10,00,000.
Can anyone help me out on this?
Here is my code sample: http://dojo.telerik.com/#jayesh-jayakumar/AtojA/8
So the problem is you are using old KendoUI version without this feature implemented. Here is a snippet with newest kendo version and it looks that it behaves diffrent for en-IN: http://dojo.telerik.com/aqEwun
However I'm not sure if this is exacly how it works in this culture cause it creates only 2 groups of digits as you can see on my example (from decimal separator it is group of 2, group of 3 and rest of digits).
EDIT:
So as you mentioned you would like to have different behavior that this in example (starting from decimal point one group of 3 digits and then groups of 2). It seems it's a bug and maybe telerik will fix it one day. Until then, you can change groupSize value in culture object from [3, 2, 0] to [3, 2] to achieve what you want.
To fix it globally in all linked cultures you can use following code:
for(var i in kendo.cultures){
var culture = kendo.cultures[i];
if(JSON.stringify(culture.numberFormat.groupSize) === '[3,2,0]'){
culture.numberFormat.groupSize = [3, 2];
}
if(JSON.stringify(culture.numberFormat.currency.groupSize) === '[3,2,0]'){
culture.numberFormat.currency.groupSize = [3, 2];
}
if(JSON.stringify(culture.numberFormat.percent.groupSize) === '[3,2,0]'){
culture.numberFormat.percent.groupSize = [3, 2];
}
}
PS. You may consider use better array comparing function than JSON.stringify().
Updated snippet: http://dojo.telerik.com/aqEwun/3

ActiveRecord count of distinct days from created_at?

Is it possible to retrieve a count of distinct records based on a field value if the field needs to be interrogated (ideally, using ActiveRecord alone)?
For example, the following returns a count of unique records based on the 'created_at' field:
Record.count('created_at', :distinct => true)
However, is it possible to get a count of, say, unique days based on the 'created_at' field in a similar way?
A naive ActiveRecord example to explain my intent being:
Record.count('created_at'.day, :distinct => true)
(I know the string 'created_at' isn't a 'Time', but that's the sort of query I'd like to ask ActiveRecord.)
You need to group the records. For example
Record.group('DATE(created_at)').count('created_at')
tells you the number of rows created on each particular date, or
Record.group('DAYOFWEEK(created_at)').count('created_at')
would tell you the number of rows created on individual days of the week.
Beware that this with the usual active record setup this will do date calculations in UTC, if you want to do your calculations in a specific timezone you'll have to add that conversion to the group statement.

How to validate a price (money amount) mySql decimal column in Yii?

I am using a mySql DECIMAL(12,4) column to hold price values (seeing how that's what Magento uses). I want to validate them in my ActiveRecord model using Yii's CValidator rules, but I'm not quite sure how to do it.
I assume I can do it with the CTypeValidator set to "float", but I wanted to see how other folks are doing this. I don't see an actual "currency" validator. Maybe I should just validate the length?
array('price', 'type', 'type'=>'float'),
or
array('price', 'length', 'max'=>17), // 12 + 4 + . = 17?
Suggestions and examples? Thanks!
I myself used the:
array('price', 'type', 'type'=>'float'),
rule... if needed, you can also use or combine the previous with the 'match' validator
array('price', 'match', 'pattern'=>'fancy regex magic here'),
To add a working example, as this is common question (with no good answer in SO)"
public function rules(){
...
array('price', 'match', 'pattern'=>'/^[0-9]{1,12}(\.[0-9]{0,4})?$/'),
...
where {1,12} is the range of whole digits , and {0,4} is the range of "sub" units.
For a normal price range of 0.01 to 9999.99 use the regex like this:
'/^[0-9]{1,0}(\.[0-9]{0,2})?$/'
ref: kitune in Yii forums
For number validation you can use:
array('price', 'numerical', 'integerOnly' => false, 'min' => 0),
If you need to restrict digits before and after decimal mark then use regex as others have suggested.

Resources