Nunber check Function failing

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
The following function checks to see if a variable read from a mysql
database is numeric. The funtion worked until I hit the value 15 303

That is a valid number but because of the space between the 15 and the
303 for (fifteen thousand three hundred and three) it errored

function is_number($number)
    $text = (string)$number;
    $textlen = strlen($text);
    if ($textlen==0) return 0;
    for ($i=0;$i < $textlen;$i++)
    { $ch = ord($text);
       if ( ($ch < 48 ) || ($ch > 57) ) return 0;
    return 1;

So I replaced the second if row with this one to check for character
32 (space) when less then charcter48

if ( (($ch < 48 ) && ($ch != 32)) || ($ch > 57) ) return 0;

But that has not really helped It now gives me this error for 15 303

Parse error: syntax error, unexpected T_LNUMBER in...(file) on line

Line 100-104 in my test are
100 print is_number(545);
101 print is_number(ca100);
102 print is_number(    );
103 print is_number(none);
104 print is_number(15 303);

I also need this to identify (ca 100) as being non-numeric it works as
above on (ca100) but not with the added space between ca and 100.

However line 102 has several spaces and that does not error but gives
"Warning: Missing argument  for is_number(), called in (file) on line
102 "

In gathering future input from users is it possible to restrict the
user from being able to type in any other characters than 0-9? I still
need to solve the number check as I am using this code to add one to
the value if conditions are met.

Any help very much appreciated

Garry Jones

Re: Nunber check Function failing

GarryJones wrote:
Quoted text here. Click to load it

you shouldn't need to write a function to check if something is a
number, use the built in PHP commands, or use regex for validation
if you want eg.

    if ( ctype_digit( "100" ) ) {
        echo "It's numerical, hallelujah";
    } else {
        echo "The devil is in our midst, NaN...";

However, what you have there really isn't a number "15 303", you would
need to replace any spaces or other chars with str_replace/preg_replace
prior to a number check, you should validate data first before it gets
into your database.

Re: Nunber check Function failing

Tyno Gendo wrote:
Quoted text here. Click to load it

You would need quotes around these anyway wouldnt you?...

eg. is_number("ca100");

Re: Nunber check Function failing

Thanks for your help.

I am a bit lost with your answer as "ctype_digit" is new to me, but I
will look into it. When I set this up last year I was not aware of the
complications of not validating. Users keyed in data in many different
ways. So now I have an enormous database with thousands of user
inputted data that I still need.

This is a control system for cycling events in Sweden and now I need
to set it up for 2008's applicants. In doing this I need to check
values users have already keyed in.

This because my form will propose new values based on last years
input. However as I can not (for instance) add one to "ca 100" in
those cases I will not open the new form for input, they will be
returned to the old form to key in correctly.

When collecting data for 2008 I am now (a little) more experienced and
can validate to skip future problems.

What I would really like is access type input validation where no
other key can be pressed when user is in the "number only fields".

So basically I am looking at two problems.
1) Last years data to be recycled if numeric,
2) Validation now needed in number only fields.

As for problem 1 let me explain.

Each organiser has to have declared how many cyclists they had in this
years events to be able to enter the details of their event for next

The number 15 303 IS valid as they have keyed in an exact number. The
value "ca 100" or "ca100" is not valid as I need exact numbers.

So I need to validate the data that is already in the mysql database
(possibly with a function?). I felt I was very close with the function
I was using if I can just get it not to test (or ignore) the space
between 15 and 303?

Of course another way of going about this is mysql code to alter
necessary and invalid values in database and I may go down that road
if I can't solve it with php.

Sorry about the long winded answer/question.

Any feedback very much appreciated.

Garry Jones

Re: Nunber check Function failing


Quoted text here. Click to load it

There's also is_numeric().

Quoted text here. Click to load it

Can be done with JavaScript, but this is _not_ a replacement for real
server-side validation.

Quoted text here. Click to load it

If users were able to insert arbitrary strings instead of numbers into
your database, then it looks like the DB design itself is broken.
Numbers should be stored in INT fields for example, not as strings.

Quoted text here. Click to load it

As already said: Use simple string functions to remove any spaces from
the submitted value, then check with is_numeric() or ctype_digit().

BTW: You need validation for _all_ fields! And you have to make sure
that the data (especially string data) is properly stored in the DB
without allowing SQL injection. mysql_real_escape_string() is the
keyword here (or PDO with prepared statements).

Quoted text here. Click to load it

So you're storing integers? Then make the DB field of type INT UNSIGNED.

Quoted text here. Click to load it

It should be possible to do that on the MySQL command line.

Quoted text here. Click to load it

What do you want to do with the invalid values?


Re: Nunber check Function failing

Quoted text here. Click to load it

You answer made great sense and I thank both of you for the time you
have spent on my problem.

Yes, I set up the database last year totally incorrectly, something I
am hoping to fix now I am a little more experienced.

For instance, a field in table_2007 can have the value 15 303 stored
as varchar. I want php to create a variable to the form with the
proposed value 15303. Then when the user presses ok, this value (or a
new one if the user so chooses) will be inserted in table_2008 (INT
UNSIGNED). Therefore I need code (poss a function similar to the one I
was trying to use) which removes the blank between 15 and 303 when the
variable that proposes the value is executed.

I feel I have learnt masses today, much obliged.

Garry Jones

Re: Nunber check Function failing

Quoted text here. Click to load it

This should do for the simple case you describe:

If you expect more complex patterns, use:


Site Timeline