Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC: VALIDATE DECIMAL (MUST BE POSITIVE)

VALIDATE DECIMAL (MUST BE POSITIVE) 13 Nov 2012 10:29 #5314

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

Just wondering how best to handle this...

I have some height and width fields to specify dimensions of products.

Requirements:
  1. enforce 2 digits after the decimal point - auto insert (.)00 if a regular integer entered e.g a 1 becomes 1.00
  2. The products should always have a height and width > 0 so 0.01 is the smallest value the user should be able to input
  3. maximum of 6digits before the decimal point
  4. If a user enters .1 it should transform value to 0.10
  5. If a user enters .12 it should transform value to 0.12
Can the 'padding' of numbers be added into the validation like in 4 & 5 or should I just force them to be entered in this way?

Here's what I have so far:
^(0|[1-9]{0,6})?(\.\d{2,2})$

Currently this allows 0.00 (not desirable) and 123456.78, 12.00 (desirable)

To illustrate further my requirements, these values should be allowed/disallowed:
AllowedDisallowed
0.010.00
123456.99012345.00
12.0012.0
119.75119.759
99.0099
Any help with this would be massively appreciated!!!

many thanks in advance!

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

Re: VALIDATE DECIMAL (MUST BE POSITIVE) 13 Nov 2012 10:45 #5318

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

Sorry to trouble you but I wondered if you had any thoughts on this?

Thanks in adv.!

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.

Re: VALIDATE DECIMAL (MUST BE POSITIVE) 13 Nov 2012 10:51 #5319

  • g1smd
  • g1smd's Avatar
  • Offline
  • Junior Member
  • RegEx fiend!
  • Posts: 31
  • Thank you received: 26
  • Karma: 6
Why do you want to force 1.00 and 2.00 but not allow 0.00?

^(0|[1-9]{0,6})?(\.\d{2,2})$
doesn't allow 10 or 20 or 205 or 250.
does allow .50 and .75 etc.

The {2,2} simplifies to {2} here.


Starting thoughts:
^  (  0  |   (  [1-9]\d{0,5} \. \d{2}  )  )  $

" 0 " OR " 1 to 9 + up to 5 digits + period + 2 digits "

Still thinking about the 0.01 to 0.99 part as that is missing.

The pattern can't "add" anything. It can merely test that the input conforms to a particular style.
Online since 1996.
Last Edit: 13 Nov 2012 17:07 by g1smd.
The administrator has disabled public write access.

Re: VALIDATE DECIMAL (MUST BE POSITIVE) 13 Nov 2012 10:56 #5321

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Because a product's width or height will never be 0.00...

UPDATED: Here's the regex I have that seems to work as expected/desired:
^(0\.0[1-9]|0\.[1-9][0-9]|[1-9]{0,6}\.\d{2,2})?$

Can this be more efficient in any way?

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.

Re: VALIDATE DECIMAL (MUST BE POSITIVE) 13 Nov 2012 10:58 #5322

  • g1smd
  • g1smd's Avatar
  • Offline
  • Junior Member
  • RegEx fiend!
  • Posts: 31
  • Thank you received: 26
  • Karma: 6
^(0\.0[1-9]|0\.[1-9][0-9]|[1-9]{0,6}\.\d{2,2})?$
doesn't allow 10 or 20 or 205 or 250.
does allow .50 and .75 etc.
doesn't allow 0 on it's own.

[0-9] simplifies to \d
{2,2} simplifies to {2}

Once you have "found" the 0 once there is no need to look for it again.
Once you have "found" the "period after the 0" once there is no need to look for it again.

Try:
^  (  0  (  \.  ( 0[1-9] | [1-9]\d )  ) ?  |  (  [1-9]\d{0,5} \. \d{2}  )  )  $
Design your pattern to parse left to right.



" 0 with optional .01 to .09 or .10 to .99 " OR " 1 to 9 + up to 5 digits + period + 2 digits "

The pattern can't "add" anything. It can merely test that the input conforms to a particular style.
Online since 1996.
Last Edit: 13 Nov 2012 17:08 by g1smd.
The administrator has disabled public write access.
The following user(s) said Thank You: JoomGuy

Re: VALIDATE DECIMAL (MUST BE POSITIVE) 13 Nov 2012 11:05 #5323

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
g1smd wrote:
^  (  0  |   (  [1-9]\d{0,5}  \.  \d{2}  )  )  $

" 0 " OR "1 to 9 + up to 5 digits and period and 2 digits"

Thinking abou the 0.01 to 0.99 part as that is missing.

The pattern can't "add" anything. It can merely test that the input conforms to a particular style.

I missed that 0.01-0.99 was missing... I was just testing it and wasn't able to validate:
0.09
0.91

But
1.01
12345.95
612345.95
were valid.

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.

Re: VALIDATE DECIMAL (MUST BE POSITIVE) 13 Nov 2012 11:05 #5324

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Is this too bloated?
^(0\.0[1-9]|0\.[1-9][0-9]|[1-9]{0,6}\.\d{2,2})?$

UPDATED:
^(0\.0[1-9]|0\.[1-9][0-9]|[1-9]\d{0,5}\.\d{2,2})?$
As the previous expression wouldn't allow 101.dd OR 123400.dd because of the [1-9] :oops:
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
Last Edit: 13 Nov 2012 11:15 by JoomGuy.
The administrator has disabled public write access.

Re: VALIDATE DECIMAL (MUST BE POSITIVE) 13 Nov 2012 11:18 #5325

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Thanks for the updates!!!

Will do L->R as you suggest. FYI everyone -
^(0(\.(0[1-9]|[1-9]\d))?|([1-9]\d{0,5}\.\d{2}))$
Works perfectly Thanks to @g1smd!!!

K+1

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: 13 Nov 2012 11:21 by JoomGuy. Reason: Added confirmation of regex
The administrator has disabled public write access.
The following user(s) said Thank You: g1smd

Re: VALIDATE DECIMAL (MUST BE POSITIVE) 13 Nov 2012 11:21 #5326

  • g1smd
  • g1smd's Avatar
  • Offline
  • Junior Member
  • RegEx fiend!
  • Posts: 31
  • Thank you received: 26
  • Karma: 6
^(0\.0[1-9]|0\.[1-9][0-9]|[1-9]\d{0,5}\.\d{2,2})?$
doesn't allow 0 on it's own.

[0-9] simplifies to \d
{2,2} simplifies to {2}

Once you have "found" the 0 once there is no need to look for it again.
Once you have "found" the "period after the 0" once there is no need to look for it again.

Try:
^  (  0  (  \.  ( 0[1-9] | [1-9]\d )  ) ?  |  (  [1-9]\d{0,5} \. \d{2}  )  )  $
Design your pattern to parse left to right.



" 0 with optional .01 to .09 or .10 to .99 " OR " 1 to 9 + up to 5 digits and period and 2 digits "

The pattern can't "add" anything. It can merely test that the input conforms to a particular style.
Online since 1996.
Last Edit: 13 Nov 2012 17:10 by g1smd.
The administrator has disabled public write access.

Re: VALIDATE DECIMAL (MUST BE POSITIVE) 13 Nov 2012 11:22 #5327

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Huh?
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.

Re: VALIDATE DECIMAL (MUST BE POSITIVE) 13 Nov 2012 11:39 #5328

  • g1smd
  • g1smd's Avatar
  • Offline
  • Junior Member
  • RegEx fiend!
  • Posts: 31
  • Thank you received: 26
  • Karma: 6
However, I would be tempted to allow the user to miss off trailing decimal digits when they enter the number
^  (  0  (  \. ( 0[1-9] | [1-9]\d? )  ) ?  |  (  [1-9]\d{0,5}  ( \. \d{1,2} )?  )  )  $
and then have a separate routine that added .00 to the end of integers (except for 0) or added a 0 to the end of one-decimal-place numbers.

This saves a lot of time on data entry but with a consequent trade off in data accuracy.

If the wanted value is 2.09 and you insist on the user typing all digits, the data will be rejected if errors are made and 2.0 or 2.9 is entered with a digit missing; however nothing can be done is 250 is typed instead of 2050 etc.

If the input allows trailing digits to be omitted, then 2.09 miskeyed as 2.0 will be "corrected" to 2.00, and input miskeyed as 2.9 will be corrected to 2.90, introducing errors in the data.

This is a trade off you have to consider.

Will the user get annoyed if they have to type 10.00 over and over again, when typing just 10 would suffice? Or should you insist on all digits every time?

Remember, data entry format doesn't have to be the same as data display format.
Online since 1996.
Last Edit: 13 Nov 2012 12:08 by g1smd.
The administrator has disabled public write access.
The following user(s) said Thank You: JoomGuy

Re: VALIDATE DECIMAL (MUST BE POSITIVE) 13 Nov 2012 12:14 #5329

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

All great points! I've given it a lot of consideration and in this instance, I want to enforce the adding of both decimals as the client will be working in cm.

However, after reading your post, I may actually split the input across 2 fields - 1 to capture the integer upto 6 digist long and the other to capture the 2digits past the point (default to 00 if none provided) and concatenate them with a decimal point. Then with a bit of jQuery, check that first is greater than 0, if not make sure the second input is in the (.)01 - (.)99 OTHERWISE transform the second into (.)00... You get my drift...

Anyway,

Thanks again!

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.

Re: VALIDATE DECIMAL (MUST BE POSITIVE) 13 Nov 2012 12:20 #5330

  • g1smd
  • g1smd's Avatar
  • Offline
  • Junior Member
  • RegEx fiend!
  • Posts: 31
  • Thank you received: 26
  • Karma: 6
Two fields might be less intuitive, the user will need to hit tab instead of point.

UI design is a complex field, not at all easy - and there's no 'one size fits all' solution. :)
Online since 1996.
The administrator has disabled public write access.
The following user(s) said Thank You: JoomGuy

Re: VALIDATE DECIMAL (MUST BE POSITIVE) 13 Nov 2012 12:50 #5331

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
I know what you mean. If I do it, they'll definitely be on the same line and the second will almost certainly be pre-filled with 00 that will be removed onFocus, re-inputted automatically if ="".

The other consideration for me is that although this client will be using cm, I will be making this dynamic through backend config to allow metres, cm, mm (all metric for the time being) so that it could work as an "Off the shelf" component. Therefore, if I go this route, I can dynamically code the second input to hide if the component has been configured to use mm as measurement.

*****ADD******
Users will also me able to mass import records from CSV to make the creation of products much simpler - given that most will already have their data in an excel file or some other format so, for the most part, I'd anticipate that they'd only be using this form to add the odd record and update existing ones.
*****************

Thanks again for everything!

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
Last Edit: 13 Nov 2012 13:11 by JoomGuy. Reason: Add
The administrator has disabled public write access.
The following user(s) said Thank You: g1smd

Re: VALIDATE DECIMAL (MUST BE POSITIVE) 13 Nov 2012 14:52 #5336

  • g1smd
  • g1smd's Avatar
  • Offline
  • Junior Member
  • RegEx fiend!
  • Posts: 31
  • Thank you received: 26
  • Karma: 6
No problem.

:)
Online since 1996.
The administrator has disabled public write access.
The following user(s) said Thank You: JoomGuy
  • Page:
  • 1
  • 2
Time to create page: 0.123 seconds

Get Started