Welcome, Guest
Username: Password: Remember me

TOPIC: [FIXED] Decimal regEx problem. JS/PHP incompatibilies

[FIXED] Decimal regEx problem. JS/PHP incompatibilies 22 Nov 2012 14:28 #5621

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hi @admin & fellow cooks,

Background
I've been playing around a little with how best to store prices. My issue is that the standard decimal type field does not give me precise enough validation over the user's input. Specifically, I need to be able to prevent the user from entering free (0 or 0.0 or 0.00) prices. Furthermore, I wanted to be able to allow users to enter or not enter decimal point and trailing decimals.

So, to achieve this I have used a custom variation of:
//decimal_10_2
^\d{0,8}(\.\d{0,2})?$
USING:
//decima8to2
^(0(\.(0[1-9]|[1-9]\d{0,1}))|([1-9]\d{0,5}(\.\d{0,2})?))?$
This allows 0 values only when followed .01 or greater or any numbers beginning with 1-9 up to 6 digits total followed by optional decimal point + 1 or 2 digits - PERFECT. BTW, This is a slight update on my post: www.j-cook.pro/forum/34-regular-expressi...mit=10&start=10#5438 (Thanks again @g1smd for your help on this!!!). In that post, I was actually storing the data as a string - NOT SO GOOD if you want to do data calculations so beware of this! + One really cool thing abut the decimal type field is that it will store trailing digits (.00) automatically if they aren't entered.

The Issue
Now, here's the weird thing... In the form, if the last thing you type into is the decimal field and you DO NOT focus out of that field before clicking save, even if the values are not valid, the save processes and 999999.99 is entered regardless of input.

To clarify this further, if I press tab or click out of the field before clicking save, the jQuery validation prevents the save as it should.

@admin, is there a bug here? I tested this against a string field with the same Regex but with a different handler however, that invalidates the save as expected.

Any help with this would be most appreciated!

many thanks,

Gez ;)
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
Last Edit: 22 Nov 2012 15:29 by JoomGuy. Reason: More descriptive title
The administrator has disabled public write access.

Decimal regEx problem. JS/PHP incompatibilies 22 Nov 2012 21:25 #5624

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Just to provide some further details about my config:
  1. Project: JStore Social
  2. Table: Products
  3. Field: Price (decimal)
  4. Layout: Products
  5. Joomla Version: 2.5
  6. Tested in: Sandbox
  7. Cook Version: 2.0
  8. JS: JQuery
  9. Form Style: Exploded
  10. Features: Maximum
  11. DB Automation: Model
  12. Timeout: 20 secs
Thanks,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Decimal regEx problem. JS/PHP incompatibilies 28 Nov 2012 10:47 #5665

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hi @admin,

Just retested this in sandbox in another table (discounts), field (value). I'm using the following cistom regex on a decimal field:
// Handler - discountpercent0pt1to99pt9
^([1-9]\d{0,1}(\.\d{1,2})?|0\.[1-9]\d{0,1})$
It allows values between 0.1 (or 0.10) to 0.99 OR 1 (.00) to 99 (.99) where values beginning with 1 or greater can have optional decimal point + 1 or 2 digits.

As previously mentioned in my first post in this thread, it will not validate correctly.

If I enter 99999 and move into the next field, the error div is not shown however, if I enter 99999.99, it does.

It invalidates correctly when trying to enter 0.01 (which is not allowed in the regex pattern) however, there is still also the strange behaviour where, no matter what is entered, if you don't focusout/click out of the field before saving the record, it always saves 99.99.

Any help with this would be massively appreciated!

Thanks,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
Last Edit: 29 Nov 2012 14:51 by JoomGuy. Reason: Rewritten
The administrator has disabled public write access.

Decimal regEx problem. JS/PHP incompatibilies 29 Nov 2012 15:14 #5690

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hi again @admin,

Just to test this further, I added a decimal field to another table stock_items to the same project. I left it as the default but added precision of 4 and scale of 2 with the auto-created COOK regex of ^\d{0,2}(\.\d{0,2})?$.

I can confirm that the same behaviours exist even when the regex has not been customised.

Thanks,

Gez

**************ADD************** - Edited to precise this (in bold)
I also just added a decimal field without any precision or scale and also without anything other than the default Regex and here's what I found...
  1. Entering the value d just saved a 0.00 (sort of expected given 'd' is the wrong type of data
  2. entering 12345678 saved 12345678.00 (as expected) BUT, here's the strange thing...
  3. Entering 123456789 saved 99999999.99
Thanks,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
Last Edit: 02 Dec 2012 10:12 by JoomGuy. Reason: Precise info
The administrator has disabled public write access.

Decimal regEx problem. JS/PHP incompatibilies 01 Dec 2012 21:31 #5705

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
I think you can try to avoid the use of \d, \s, \w, etc...

Because these statements are not recognized in JS.

Cook try to convert them, but sometimes it is not possible.
Nothing to do with that... Replacing \d to [0-9] is not always the same.

For instance : ...^[0-9] for a NOT is not working.... If I remember well.

So, try to work on regEx for javascript, then it will work on PHP.

I cannot keep this ticket open because I must clean a little bit the stack list and I think it is not a cook problem.


Before posting regex in this category, can you test your outputed (generated) javascript RegEx and test it separately ?
Coding is now a piece of cake
The administrator has disabled public write access.

Decimal regEx problem. JS/PHP incompatibilies 02 Dec 2012 08:18 #5707

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hi Jocelyn,

Thanks for the reply...
admin wrote:
...So, try to work on regEx for javascript, then it will work on PHP.
This is not a RegEx issue at all. The regular expression works perfectly - tested in plain javascript @ regexpal.com/ - please try it yourself, you'll see that javascript has no issues matching against this expression. Please place
^([1-9]\d?(\.\d{1,2})?|0\.[1-9]\d?)$
in the Regex input then try to type a decimal < 0.1 (0.01 for instance) or one > 99.99 and it will NOT get highlighted yellow. However, enter anything between 0.1 and 99.99 the entered values will get highlighted yellow

Also, in a string (varchar) type field in cook it works perfectly.

Additionally, as you can see from my last post, I tested it with just the most basic decimal field setup with NO precision or scale and I get the same issue.

Just to clarify, the only reason I shared the RegEx in the post was for others that might want to use it, NOT because the issue is related to it. This is very definitely an issue with validation of the decimal field type and therefore, I will move back to tickets but leave @g1smd 's improvements here as they aren't related to the ticket.

Thanks,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
Last Edit: 02 Dec 2012 10:13 by JoomGuy.
The administrator has disabled public write access.

Decimal regEx problem. JS/PHP incompatibilies 02 Dec 2012 09:26 #5709

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hi @admin

Have you tried a decimal field in a cook 2.0 project with jquery OR
My project (jstore social)?

You'll see what I mean... All references to which table, layout & field are in the thread - even the new tests I've added.

Thanks,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Decimal regEx problem. JS/PHP incompatibilies 02 Dec 2012 12:02 #5718

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Please I'm pretty desperate to resolve this issue as a decimal field is absolutely required in my project for use with prices & percentages.

As I said in my other posts, I've done a lot of testing and can confirm that this is definitely a cook issue, not a regex problem as I've tested the JS regex with no issues at all (also, the builder regex checker works/behaves exactly correctly to validate/invalidate input).

Many thanks,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Decimal regEx problem. JS/PHP incompatibilies 02 Dec 2012 12:10 #5719

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Sorry for beeing late,
I had to check all posts in the stack list.

I read well, and I think I understand.
It looks like an overflooding of the float type.

I think that a (float)$myValue, can prevent this before to save, but it is only my first intuition.

I try to check it today. Promise.
Coding is now a piece of cake
The administrator has disabled public write access.

Decimal regEx problem. JS/PHP incompatibilies 02 Dec 2012 12:15 #5720

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hi @admin,

Great thanks, I really appreciate it... It's not very often I find anything wrong ;)

Just to clarify regarding your message:admin wrote:
It looks like an overflooding of the float type.

I think that a (float)$myValue, can prevent this before to save, but it is only my first intuition.
You do mean in a decimal field, right? Is it dealt with as a float before save?

Many thanks,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Decimal regEx problem. JS/PHP incompatibilies 02 Dec 2012 12:25 #5721

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
I just finish something else before.

Just a question, because I do not remember...
Is your save query sending the float in a quoted string ? Because it mustn't...
Must be unquoted, AND with a point '.', not a comma of course. (french and german)
Coding is now a piece of cake
The administrator has disabled public write access.

Decimal regEx problem. JS/PHP incompatibilies 02 Dec 2012 12:53 #5722

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hi Jocelyn,

I don't believe I have an explicit save() routine - I think it's just using the parent/super class...

Just to be clear, this is a decimal, not a float field... Does it still matter with a decimal or is that what you meant when you refer to float?

I think the issue might be both sides - client and in php because, adding 0.01 will prevent save with jQuery validation, but, entering 999 won't.

Thanks,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
Last Edit: 02 Dec 2012 13:06 by JoomGuy.
The administrator has disabled public write access.

Decimal regEx problem. JS/PHP incompatibilies 02 Dec 2012 13:04 #5723

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
I just checked, I don't have a save in there, it must be using the parent.

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Decimal regEx problem. JS/PHP incompatibilies 02 Dec 2012 13:24 #5724

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
First thing I can see is that you are using a precision of 8.
It means 8 digits max.

so 123456789 is not allowed.

So, you should validate the length in regEx, because for an integer, it is not possible to round the value.
Coding is now a piece of cake
The administrator has disabled public write access.

Decimal regEx problem. JS/PHP incompatibilies 02 Dec 2012 13:31 #5725

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Sorry, I had changed to 4,2
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.
Time to create page: 0.131 seconds

Get Started