Click to See Complete Forum and Search --> : validating my data from form submissions


Jaime1985
05-13-2005, 03:44 AM
Hi everyone

Is there a way you can validate/strip values once someone has clicked the Submit button on a form?

As an example, if I enter into a textbox the value: $2000
This value will be posted to on to script file that puts it into a database.

Is it possible so that the $ sign gets stripped out before its passed onto the database?
I don't particularly want an error message hitting the user asking him to enter it again because he included the $ sign. I need something that happens behind the scenes that looks at the value he entered and strips out anything that isn't a number and then simply passes it through to the database.

I'm being so picky because for the sake of my data integrity, some users are entering the '$' sign and some aren't. Some are entering a ',' symbol, which also has the same affect. This is annoying when it comes to sorting the data and reporting. If anyone can help, be much appreciated :-)

James

NogDog
05-13-2005, 10:41 AM
See if this works:

$value = "$2000";
$value = ltrim($value, "$");
echo $value;

Natcon67
05-13-2005, 12:02 PM
you could also use this:

$value = "$2000";
$value = str_replace("$","",$value);

then you could create multiple str_replaces with all of the characters that you want to take out.

NogDog
05-13-2005, 01:07 PM
You could get rid of everything except numbers and dots (assuming you want to allow cents) with:

$value = preg_replace("/[^0-9\.]/", "", $value);