Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Sign In
Not a member yet? Click here to register.

SOLVED: isnum function located on maincore.....

Noticed that we have this function and thought it was handy as it is shorter than is_numeric.... but it works differently...

Maybe if we change the regex to something more accomodating, that perhaps included decimal numbers.... like maybe:



or stop doing preg_match altogether and run is_numeric inside of isnum.... ??

I am not certain about the rationale behind the isnum function and why it avoids matching decimal values... could that be something that was left out for some reason?

I for one, modded it to the regex posted above because I like the shorter input time and I'm working with currency calculations so I need it to validate decimal values that a user will be putting on a form.
Good point. I'll move it to RC 7.03 thread.
Pretty sure is a reason why it does things the way it does it. You'll have to look through the code 1st and make sure changing the function doesn't pose a security risk. Also you have the is_numeric function for decimals why not use it instead?
I was building an infusion and for consistency's sake I wanted to use isnum as it is what is normally used in the cms for that purpose.

But as I was working with decimals noticed the odd regex.... decimals are numbers too... so... why not exted it !? I'm pretty sure it is to enforce non-decimal numbers on fields like tinyint and stuff like that, but normally values get truncated and no errors are shot when we try saving decimal values on non-decimal fields... so there would be no harm done I think... also the proposed regex, validates a proper decimal .... the only fix I'd add is for it to also not use the decimal point.... changing {1} into {0,1} next to the \. so it matches non-decimal and decimal alike.

Again, I'd get rid of the preg_match in isnum altogether and use is_numeric ....... but I am not sure if there is a reason for that to be using regular expressions.... seems really odd though... just saying....
We could add another argument, $decimals set by default as FALSE, so everything will work as before but you get the option to use decimals if you want to.
That would work too. The gist of it would be to not treat decimals as if they weren't numbers. lol
Just fixed the regex, which in principle, was good intentioned, but wrong.

Here is the correct regex that should be used to match decimals, and integers:
function isnum($value) {
   if (!is_array($value)) {
      return (preg_match("/^[0-9]+(\.{0,1})[0-9]*$/", $value));
   } else {
      return false;

I slipped there and sent it with [0-9]+ in the second number grp where it should have been [0-9]*

With + after [0-9] we are saying one or more of 0-9.
With * after [0-9] we are saying zero or more of 0-9.

We already have 0 or 1 of the dot, for the decimal, with \.{0,1}
and only one or more digits in the first group as it was before. Now it matches perfectly for the purpose of including decimals in the isnum matching.

Taking JoiNNN's input into account... here would be a version with an argument that would switch the matching behavior:
function isnum($value, $decimal=false) {
   if (!is_array($value)) {
      if($decimal==true) return (preg_match("/^[0-9]+(\.{0,1})[0-9]*$/", $value));
      return (preg_match("/^[0-9]+$/", $value));
   } else {
      return false;

Well Elven, I've given consideration. This allows us to solidify the price validation functions for defender later. Good work and thanks for the code. :G

Just updated with your function: https://github.com/php-fusion/PHP-Fusion/commit/473acec42ddfb84e06e625bcfdc4de90bca45705
Thread Information
8 posts
562 times
Last Post
Last updated on 5 years ago
You can view all discussion threads in this forum.
You can start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You cannot download attachments in this forum.