Welcome Guest, Not a member yet? Register   Sign In
Numeric Form Validation with NEGATIVE number...
#1

[eluser]wavefade[/eluser]
I'm trying to validated a numeric field and when I put in a negative number it tells me it must be numeric. I'm using the rapyd component library for datagrids and dataforms on thsi project. I understand that the form validation is built into CI. What can I do to solve this problem?

thanks
#2

[eluser]gtech[/eluser]
Hello I looked at the documentation and can't find an option for validating signed integers.

if you look in libraries/validation.php the numeric function is a simple regular expression. you can simply change this to deal with negative and positive integers... e.g.

validation.php
Code:
...
  /**
  * Numeric
  *
  * @access    public
  * @param    int
  * @return    bool
  */    
  function numeric($str)
  {
    // original code..
    // return ( ! ereg("^[0-9\.]+$", $str)) ? FALSE : TRUE;
  
    return ( ! ereg("^\-*[0-9\.]+$", $str)) ? FALSE : TRUE;
  }
...

or add a new function.. maybe a feature request?
#3

[eluser]schnoodles[/eluser]
You could always just do

$rule['field'] = 'is_numeric'; and use php's one which supports negatives.
#4

[eluser]wavefade[/eluser]
[quote author="gtech" date="1193894252"]Hello I looked at the documentation and can't find an option for validating signed integers.

if you look in libraries/validation.php the numeric function is a simple regular expression. you can simply change this to deal with negative and positive integers... e.g.

validation.php
Code:
...
  /**
  * Numeric
  *
  * @access    public
  * @param    int
  * @return    bool
  */    
  function numeric($str)
  {
    // original code..
    // return ( ! ereg("^[0-9\.]+$", $str)) ? FALSE : TRUE;
  
    return ( ! ereg("^\-*[0-9\.]+$", $str)) ? FALSE : TRUE;
  }
...

or add a new function.. maybe a feature request?[/quote]

Thanks a bunch! I used this little fix and it works great.




Theme © iAndrew 2016 - Forum software by © MyBB