Forum Moderators: coopster

Message Too Old, No Replies

Stripping Chars before SQl insertion

         

adammc

2:42 am on Sep 13, 2006 (gmt 0)

10+ Year Member



Hi Guys,

I was hoping for a little help if possible?

I have a form with an input box that the user puts in a dollar amount. I dont want them to put a dollar sign or anything else apart from the currency amount (1.00, 1,658 etc)

How would I achieve this?

adammc

4:15 am on Sep 13, 2006 (gmt 0)

10+ Year Member



I got is sorted, thanks anyway.

I decided to go with something like this:

[PHP]// check that the price is the correct format - no dollar signs
if (!is_numeric($price)) {

$errors[] = 'Please input price in the form 123.45, without any other symbols.';
}[/PHP]

dreamcatcher

5:11 am on Sep 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might want to read up on the is_numeric function:


Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. Thus +0123.45e6 is a valid numeric value. Hexadecimal notation (0xFF) is allowed too but only without sign, decimal and exponential part.

[uk.php.net...]

For better checking, try the following:

[uk.php.net...]

dc

omoutop

5:59 am on Sep 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



plus there are plenty of java scripts that limit the user's input to just numbers

whoisgregg

3:35 pm on Sep 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A combination of javascript and PHP should do the trick:

Javascript:

<input type="text" name="money" value="0.00" onkeyup="this.value = this.value.replace(/[^.,0-9]/g, '');" />

PHP:

$test_string = "USD $3.20 Please!";
echo preg_replace("/[^.,0-9]/e", "", $test_string); // 3.20

Added: There's probably a few dozen methods of doing this though, these are just my recent favorites. :)