Validation of forms

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pinkygal
Forum Newbie
Posts: 8
Joined: Tue Jan 21, 2003 7:54 am

Validation of forms

Post by pinkygal »

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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

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.:

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
}
(note: in the code above && should be replaced with && - I'll try and get that bug fixed soon)

Mac
pinkygal
Forum Newbie
Posts: 8
Joined: Tue Jan 21, 2003 7:54 am

Validation of forms

Post by pinkygal »

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;
}
}
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post by fractalvibes »

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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

In PHP, all form variables should be available just by prepending a '$' in front of the name.
ONLY if register_globals is off, which is no longer the default.

For more info:
viewtopic.php?t=511

Mac
User avatar
9902468
Forum Commoner
Posts: 89
Joined: Thu Jun 06, 2002 6:39 am
Location: Europe

Post by 9902468 »

One should always use js to validate form fields. It really can mean a lot to user, that response is little quicker. Also if the code is done correctly there should be close to none compatibility problems /w browsers.
Post Reply