473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Nunber check Function failing

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{$i});
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
104

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
Sweden

Sep 28 '07 #1
6 2045
GarryJones wrote:
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{$i});
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
104

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
Sweden
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.
Sep 28 '07 #2
Tyno Gendo wrote:
>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);
You would need quotes around these anyway wouldnt you?...

eg. is_number("ca100");
Sep 28 '07 #3
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
year.

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
Sweden
Sep 28 '07 #4
..oO(GarryJones)
>I am a bit lost with your answer as "ctype_digit" is new to me, but I
will look into it.
There's also is_numeric().
>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".
Can be done with JavaScript, but this is _not_ a replacement for real
server-side validation.
>So basically I am looking at two problems.
1) Last years data to be recycled if numeric,
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.
>2) Validation now needed in number only fields.
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).
>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
year.

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 you're storing integers? Then make the DB field of type INT UNSIGNED.
>So I need to validate the data that is already in the mysql database
(possibly with a function?).
It should be possible to do that on the MySQL command line.
>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?
What do you want to do with the invalid values?

Micha
Sep 28 '07 #5
What do you want to do with the invalid values?

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
Sweden

Sep 28 '07 #6
On Sep 28, 1:51 pm, GarryJones <mor...@algonet.sewrote:
What do you want to do with the invalid values?

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
Sweden
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.
This should do for the simple case you describe:
http://www.php.net/manual/en/function.str-replace.php

If you expect more complex patterns, use:
http://www.php.net/manual/en/function.preg-replace.php

Tom

Sep 28 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: saayan | last post by:
Hi, I am using PHP 5.0.1 with Apache 2 on Win XP (SP2). My index.php file has require_once contents.php and also for functions.php. My contents.php file also has a require_once for...
9
by: simonmarkjones | last post by:
I want to call a function which does this when the next record button is pressed (calling it from before update) if textboxes are empty then Message box you must fill text box
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
44
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
0
by: Borse, Ganesh | last post by:
Hi, My following code is failing with an error of "isSizeSmall not function or callable" //--------------------------------------------------- char szExpr; memset(szExpr,'\0',sizeof(szExpr));...
4
by: ciccio | last post by:
Dear all, once again I stumbled upon the following puzzling problem. When having the following two files (see below), the gnu compiler compiles the file without a problem while the compiler...
55
by: lovecreatesbea... | last post by:
Do you check all error conditions for all library calls in you code? Is it necessary to check all errors? Is it convenient to check all errors (or how to make code clean and readable with mass of...
13
by: PhpCool | last post by:
Hi, since sometime I'm stuck in a problem where I want to check or uncheck all the checkboxes. If I'm choosing name for the checkbox array as 'chkbx_ary' then I'm able to check/uncheck all the...
1
by: Anuj | last post by:
Hi, since sometime I'm stuck in a problem where I want to check or uncheck all the checkboxes. If I'm choosing name for the checkbox array as 'chkbx_ary' then I'm able to check/uncheck all the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.