Home > Archive > PHP Language > March 2006 > Validation on input amount
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Validation on input amount
|
|
| Stefan van Roosmalen 2006-03-28, 6:57 pm |
| Hi there,
I would like to check an amount which have been entered via a webform.
How can I do this?
Right --> 2
Right --> 150,99
Wrong --> 2,2
Wrong --> ,2
Wrong --> 3,44832
I am using the comma as the decimals seperator, and it must be an amount
with NO decimals, or with 2 decimals.
Please help.
gr.,
Stefan.
| |
| Juliette 2006-03-29, 7:57 am |
| Stefan van Roosmalen wrote:
> Hi there,
>
> I would like to check an amount which have been entered via a webform.
> How can I do this?
>
> Right --> 2
> Right --> 150,99
>
> Wrong --> 2,2
> Wrong --> ,2
> Wrong --> 3,44832
>
> I am using the comma as the decimals seperator, and it must be an amount
> with NO decimals, or with 2 decimals.
>
> Please help.
>
> gr.,
> Stefan.
>
>
Stefan,
Try something along the lines of this (untested):
if( isset($_POST['amount'] && $_POST['amount'] !== '' ) {
$amount = explode(',', $_POST['amount']);
if ( ctype_digit( $amount[0] ) === FALSE ) {
echo 'the number before the comma was not a digit';
}
elseif ( ( isset( $amount[1] ) && $amount[1] !== '' ) && ctype_digit(
$amount[1]) === FALSE ) {
echo 'the number after the comma was not a digit';
}
elseif ( strlen( $amount[1] ) !== 2 ) {
echo 'the number after the comma does not have two digits';
}
else {
echo 'correctly formatted number';
}
}
else {
echo 'no amount was filled in';
}
Good luck,
Juliette
| |
|
| On Wed, 29 Mar 2006 15:16:20 +0200, Juliette wrote:
> Stefan van Roosmalen wrote:
>
> Stefan,
>
> Try something along the lines of this (untested):
>
> if( isset($_POST['amount'] && $_POST['amount'] !== '' ) {
>
> $amount = explode(',', $_POST['amount']);
>
> if ( ctype_digit( $amount[0] ) === FALSE ) {
> echo 'the number before the comma was not a digit';
> }
> elseif ( ( isset( $amount[1] ) && $amount[1] !== '' ) && ctype_digit(
> $amount[1]) === FALSE ) {
> echo 'the number after the comma was not a digit';
> }
> elseif ( strlen( $amount[1] ) !== 2 ) {
> echo 'the number after the comma does not have two digits';
> }
> else {
> echo 'correctly formatted number';
> }
> }
> else {
> echo 'no amount was filled in';
> }
> }
>
> Good luck,
> Juliette
Why make simple when you can make it hard?
Why shouldnt'we reinvent the wheel?
johand@horus:~/src/php$ cat amount.php
<?php
if($argc >1)
{
$cnt = 1;
while( $theArg = $argv[$cnt++])
{
print(ereg('^[1-9][0-9]*(,[0-9][0-9])*$', $theArg) ? $theArg . ": OK\n" : $theArg . ": NOT ok\n");
}
}
?>
johand@horus:~/src/php$ php amount.php 1 1,1 1,13 1,34 ,27 3.14 ab1,14
1: OK
1,1: NOT ok
1,13: OK
1,34: OK
,27: NOT ok
3.14: NOT ok
ab1,14: NOT ok
Johan
|
|
|
|
|