PHP Form Validation

Using the validation script:

Example:

Form:

 <form id="cefiform" name="cefiform" action="<?php echo(htmlentities($_SERVER["PHP_SELF"]));?>" method="post" accept-charset="UTF-8">
   User Name: <input type="text" id="username" name="username" value="<?php if(isset($_POST["username"])) echo($_POST["username"]);?>">
  First Name: <input type="text" id="firstname" name="firstname" value="<?php if(isset($_POST["firstname"])) echo($_POST["firstname"]);?>">
   Last Name: <input type="text" id="lastname" name="lastname" value="<?php if(isset($_POST["lastname"])) echo($_POST["lastname"]);?>">
      E-Mail: <input type="text" id="email" name="email" value="<?php if(isset($_POST["email"])) echo($_POST["email"]);?>">
      Gender: <input type="radio" id="male" name="gender"<?php if(isset($_POST["gender"]) && ($_POST["gender"] == "male")) echo(' checked="checked"');?> value="male"><label for="male">Male</label>
              <input type="radio" id="female" name="gender"<?php if(isset($_POST["gender"]) && ($_POST["gender"] == "female")) echo(' checked="checked"');?> value="female"><label for="female">Female</label>
 </form>

PHP Script:

 if(isset($_POST))
 {
  // Form posted, perform validation tests.
  // Create validator object
  $validator = new Validator();
  // Move $_POST values to validator source
  $validator->addSource($_POST);
  // Trim whitespace from both ends of validator source
  $validator->trimSource();
  // Add validation rules

  if($validator->isFormValid())
  {
   // Data validated and sanitized, now return data from the sanitized source
   $_POST = $validator->returnSource();
   // Continue with processing and/or database operations
  }
 }

Custom Validations:

A custom validation function must return true or false.

Format:

$validator->addValidation('Function Name', 'Element Name', 'Error Message'[, array(Arg1[, Arg2, ...])]);
Descriptor Applies To Type What Descriptor Does
Creative Electronic Formulas, Inc. - ©2014-2017
alnum OR alphanumeric text Allows only alphabetic and numeric characters. (NOTE: space or punctuation not allowed)
alnum_d OR alphanumeric_dash text Allows only alphabetic, numeric, dash(-) and underscore(_) characters.
alnum_s OR alphanumeric_space text Allows only alphabetic, numeric and space characters.
alpha OR alphabetic text Allows only alphabetic characters.
alpha_d OR alphabetic_dash text Allows only alphabetic, dash(-) and underscore(_) characters.
alpha_s OR alphabetic_space text Allows only alphabetic and space characters.
crcard text Validates credit card numbers.
check OR checked checkbox Indicates the element must be checked.
date_f text Allows only numeric, period(.), dash(-) and slash(/) characters. (NOTE: Doesn't validate format or date)
email text Validates e-mail address format. (NOTE: Doesn't test if e-mail address is working)
eq=? OR equal=? number Indicates the element value must be equal to ? value.
eqelmnt=name text Indicates the element value must be equal to element name value.
exlen=? OR exact_length=? text, number Indicates the element value length must be exactly ? characters.
geelmnt=name number Indicates the element value must be greater than or equal to the element name value.
gtelmnt=name number Indicates the element value must be greater than the element name value.
gt=? OR greater_than=? number Indicates the element value must be greater than ? value.
gte=? OR greater_than_equal=? number Indicates the element value must be greater than or equal to ? value.
int OR integer number Allows only integer(whole number) characters.
ip text Validates an IP address format.
ipv4 text Validates an IP4 address format.
ipv6 text Validates an IP6 address format.
lcase OR lowercase text Indicates the element value will be converted to lower case. (NOTE: The error message parameter is not used)
leelmnt=name number Indicates the element value must be less than or equal to the element name value.
ltelmnt=name number Indicates the element value must be less than the element name value.
lt=? OR less_than=? number Indicates the element value must be less than ? value.
lte=? OR less_than_equal=? number Indicates the element value must be less than or equal to ? value.
maxlen=? OR max_length=? text, number Indicates the element value length must not be greater than ? characters.
minlen=? OR min_length=? text, number Indicates the element value length must be at least ? characters.
neelmnt=name text, number Indicates the element value must not be equal to element name value.
nochk OR nocheck checkbox Indicates the element must not be checked.
nosel=? OR noselect=? select Indicates the element value must not be equal to ? value.
padb=? OR padboth=? text, number Indicates the element value will be padded on both sides to the ? length. (NOTE: The error message parameter is used to specify the character to pad with)
padl=? OR padleft=? text, number Indicates the element value will be padded on the left side to the ? length. (NOTE: The error message parameter is used to specify the character to pad with)
padr=? OR padright=? text, number Indicates the element value will be padded on the right side to the ? length. (NOTE: The error message parameter is used to specify the character to pad with)
req OR required text, number Indicates the element value is required.
selrad OR select_radio radio Indicates the element(radio) value is required.
ucase OR uppercase text Indicates the element value will be converted to upper case. (NOTE: The error message parameter is not used)
url text Validates a URL.