Validation of forms
Moderator: General Moderators
Validation of forms
current I am developing a financial calculator and I need to do some validation to all the fields. Does anybody have any idea how to validate forms to prevent user from entering a negative number and other values instead of numbers?
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
You can test to make sure that the value is greater than 0, that'll get rid of the problem of negative numbers. The is_numeric() function can be used to test that the input is a number.
E.g.:
(note: in the code above && should be replaced with && - I'll try and get that bug fixed soon)
Mac
E.g.:
Code: Select all
if (is_numeric($_POST['data']) && $_POST['data'] >= 0) {
// Is number and greater than or equal to zero
} else {
// Is not number or is less than zero
}Mac
Validation of forms
i had tried the codes given. But currently I have 3 fields to validate. I have tried using loop but it gives me error. what exactly must i put in the 'data' field? must I put $i or the form name?
for($i=0;$i<4;$i++){
if (is_numeric($_POST['$i']) && $_POST['$i'] >= 0)
// Is number and greater than or equal to zero
{
$Interestpermth=($Interest/12);
$Interestpayable=($Interestpermth*$Loan)/100;
$PrincipalAmount=($Loan/$Period);//amount that user need to pay each month
$Payments=$PrincipalAmount+$Interestpayable;//Total amount user has to pay monthly
$Payments=round($Payments,2);
//Print the results
print("<b><font face=\"verdana\" size=\"2\">The interest rate per month is $Interestpermth\n<P>");
print("<b><font face=\"verdana\" size=\"2\">The interest payable per month is $interestpayable\n<P>");
print("<b><font face=\"verdana\" size=\"2\">The total amount that you have to pay is \$$PrincipalAmount\n<P>");
print("<b><font face=\"verdana\" size=\"2\">You must pay \$$Payments monthly.\n<P>");
break;
}else
{
// Is not number or is less than zero
print("<b><font face=\"verdana\" size=\"2\">Please enter a valid number!");
break;
}
}
for($i=0;$i<4;$i++){
if (is_numeric($_POST['$i']) && $_POST['$i'] >= 0)
// Is number and greater than or equal to zero
{
$Interestpermth=($Interest/12);
$Interestpayable=($Interestpermth*$Loan)/100;
$PrincipalAmount=($Loan/$Period);//amount that user need to pay each month
$Payments=$PrincipalAmount+$Interestpayable;//Total amount user has to pay monthly
$Payments=round($Payments,2);
//Print the results
print("<b><font face=\"verdana\" size=\"2\">The interest rate per month is $Interestpermth\n<P>");
print("<b><font face=\"verdana\" size=\"2\">The interest payable per month is $interestpayable\n<P>");
print("<b><font face=\"verdana\" size=\"2\">The total amount that you have to pay is \$$PrincipalAmount\n<P>");
print("<b><font face=\"verdana\" size=\"2\">You must pay \$$Payments monthly.\n<P>");
break;
}else
{
// Is not number or is less than zero
print("<b><font face=\"verdana\" size=\"2\">Please enter a valid number!");
break;
}
}
-
fractalvibes
- Forum Contributor
- Posts: 335
- Joined: Thu Sep 26, 2002 6:14 pm
- Location: Waco, Texas
Really I think it best to validate on both the clientside(javascript) and serverside(PHP).
As I always have a mix of fields, I usually treat each field individually, rather than trying to iterate through all the form fields - seems the most foolproof method.
Likewise, you can test for numeric in Javascript:
if (isNaN(document.myform.myfield.value)) {
alert("Not Numeric!")
}
In PHP, all form variables should be available just by prepending a '$' in front of the name. <hint> develop a standard naming convention for all the various input type fields -i.e.
textfield - txtMyVar - PHP : $txtMyVar
Select - lstMyVar
submit button - btnSubmit
checkbox - ckMyVar
radio - rdoMyVar
Validating first in Javascript saves a roundtrip back to the server. Validating in PHP guarantees validity, in case they have javascript disabled, or an ancient browser.
You could also consider writing a few functions in each and pass in references to the variables to save a little coding. Say:
A presence check for required fields
A numeric test
A > zero test
Email validity
Range checks - i.e. txtSomeField should be less than 500...whatever
Above all - whenever you can populate a form field with a set of predetermined ranges of values, do so - i.e. a drop-down list of states rather than have them enter TX and make a typo and enter TZ!
Phil J.
As I always have a mix of fields, I usually treat each field individually, rather than trying to iterate through all the form fields - seems the most foolproof method.
Likewise, you can test for numeric in Javascript:
if (isNaN(document.myform.myfield.value)) {
alert("Not Numeric!")
}
In PHP, all form variables should be available just by prepending a '$' in front of the name. <hint> develop a standard naming convention for all the various input type fields -i.e.
textfield - txtMyVar - PHP : $txtMyVar
Select - lstMyVar
submit button - btnSubmit
checkbox - ckMyVar
radio - rdoMyVar
Validating first in Javascript saves a roundtrip back to the server. Validating in PHP guarantees validity, in case they have javascript disabled, or an ancient browser.
You could also consider writing a few functions in each and pass in references to the variables to save a little coding. Say:
A presence check for required fields
A numeric test
A > zero test
Email validity
Range checks - i.e. txtSomeField should be less than 500...whatever
Above all - whenever you can populate a form field with a set of predetermined ranges of values, do so - i.e. a drop-down list of states rather than have them enter TX and make a typo and enter TZ!
Phil J.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
ONLY if register_globals is off, which is no longer the default.In PHP, all form variables should be available just by prepending a '$' in front of the name.
For more info:
viewtopic.php?t=511
Mac