lansing
06-04-2006, 12:44 AM
I have an input box that needs to be nothing but numbers 1,2,3,4,5,6,7,8,9,0 & nothing else except for a single . period!! This input box is for currency amount...I don't think anybody is going to enter any other characters other than numbers & a period, but I know somebody will & it will mess up my inputs.
I am using JavaScript as the first way to make sure the format is the way I want it, but I need a way of checking using php for my user's that have JavaScript disable. I will post my JavaScript code below in case you need to see it.
The reason I MUST get everything but numbers & a single period out is because I am using these inputs to do some simple math before inserting in the DB.
This is the php code I have got so farr, but this is very very basic & not enough to do what I need done. Is there some script that I could use that would strip EVERY CHARACTER, but numbers & a single period?$strip_this = array("$", ",");
$striped_data = str_replace($strip_this, "", "$amount");
My JavaScript code function validate(field) {
var valid = ".0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Invalid format. Do not enter dollar signs or commas!");
field.focus();
//field.select();
}
}
Thanks!
I am using JavaScript as the first way to make sure the format is the way I want it, but I need a way of checking using php for my user's that have JavaScript disable. I will post my JavaScript code below in case you need to see it.
The reason I MUST get everything but numbers & a single period out is because I am using these inputs to do some simple math before inserting in the DB.
This is the php code I have got so farr, but this is very very basic & not enough to do what I need done. Is there some script that I could use that would strip EVERY CHARACTER, but numbers & a single period?$strip_this = array("$", ",");
$striped_data = str_replace($strip_this, "", "$amount");
My JavaScript code function validate(field) {
var valid = ".0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Invalid format. Do not enter dollar signs or commas!");
field.focus();
//field.select();
}
}
Thanks!