There are 4 types of validators:
- 1. KoolRequiredFieldValidator : Check if a field is not empty
- 2. KoolRangeValidator : Check if value is within a defined range.
- 3. KoolRegularExpressionValidator : Check if field value is conformed to regular expression
- 4. KoolCustomValidator : Provide your own custom client-side validation.
To use KoolRequiredFieldValidator, you do as follow:
- 1. Create object and add to form: $txtName_RequiredFieldValidator = $myform_manager->AddControl(new KoolRequiredFieldValidator("txtName_RequiredFieldValidator"));
- 2. Set which field to validate: $txtName_RequiredFieldValidator->TargetId = "txtName";
- 3. Set error message which will be show when validation is failed: $txtName_RequiredFieldValidator->ErrorMessage = "The textbox can not be empty!";
- 4. Render the validator at your html place:
<?php echo $txtName_RequiredFieldValidator->Render();?>
* In the step 4, if you define the form with $myform_manager->RenderWithExistingMarkup = true;, you do not above render. Instead, you create a span in your html like this:
<span id="txtName_RequiredFieldValidator"><span>
The way of iniitiating KoolRangeValidator, KoolRegularExpressionValidator is the same. However,
- 1. In the KoolRangeValidator, you provide value for $MinValue and $MaxValue properties.
- 2. In the KoolRegularExpressionValidator, you provide value for $Expression property.
if you are using KoolCustomValidator, you will provide the name of your custom client-side validator function for $ClientValidationFunction property.
Your custome validation function is an javascript function which returns true if valid and returns false if not valid.
<?php
require $KoolControlsFolder."/KoolAjax/koolajax.php";
require $KoolControlsFolder."/KoolGrid/koolgrid.php";
require $KoolControlsFolder."/KoolForm/koolform.php";
$koolajax->scriptFolder = $KoolControlsFolder."/KoolAjax";
$myform_manager = new KoolForm("myform");
$myform_manager->scriptFolder = $KoolControlsFolder."/KoolForm";
$myform_manager->DecorationEnabled = true;
$myform_manager->styleFolder = "sunset";
//Use the numeric textbox for Age
$txtAge = $myform_manager->AddControl(new KoolNumericTextBox("txtAge"));
//Use the masked textbox for phone
$txtPhone = $myform_manager->AddControl(new KoolMaskedTextBox("txtPhone"));
$txtPhone->Mask = "(###)-######";
$txtPhone->SelectionOnFocus = "CaretToBeginning";
//Create the required field validator for txtName
$txtName_RequiredFieldValidator = $myform_manager->AddControl(new KoolRequiredFieldValidator("txtName_RequiredFieldValidator"));
$txtName_RequiredFieldValidator->TargetId = "txtName";
$txtName_RequiredFieldValidator->ErrorMessage = "The textbox can not be empty!";
//Create the required field validator and range validator for txtName
$txtAge_RequiredFieldValidator = $myform_manager->AddControl(new KoolRequiredFieldValidator("txtAge_RequiredFieldValidator"));
$txtAge_RequiredFieldValidator->TargetId = "txtAge";
$txtAge_RequiredFieldValidator->ErrorMessage = "Please, select an year number!";
$txtAge_RangeValidator = $myform_manager->AddControl(new KoolRangeValidator("txtAge_RangeValidator"));
$txtAge_RangeValidator->TargetId = "txtAge";
$txtAge_RangeValidator->ErrorMessage = "Year number should be a non negative less than 50.";
$txtAge_RangeValidator->MinValue=0;
$txtAge_RangeValidator->MaxValue=49;
//Create the required field validator and regular expression validator for txtPhone
$txtPhone_RequiredFieldValidator = $myform_manager->AddControl(new KoolRequiredFieldValidator("txtPhone_RequiredFieldValidator"));
$txtPhone_RequiredFieldValidator->TargetId = "txtPhone";
$txtPhone_RequiredFieldValidator->ErrorMessage = "Please enter phone number!";
$txtPhone_RegularExpressionValidator = $myform_manager->AddControl(new KoolRegularExpressionValidator("txtPhone_RegularExpressionValidator"));
$txtPhone_RegularExpressionValidator->TargetId = "txtPhone";
$txtPhone_RegularExpressionValidator->Expression = "/\d{9}/";
$txtPhone_RegularExpressionValidator->ErrorMessage = "Format is (###)-######";
//Create the required field validator and regular expression validator for txtEmail
$txtEmail_RequiredFieldValidator = $myform_manager->AddControl(new KoolRequiredFieldValidator("txtEmail_RequiredFieldValidator"));
$txtEmail_RequiredFieldValidator->TargetId = "txtEmail";
$txtEmail_RequiredFieldValidator->ErrorMessage = "Please, enter an e-mail!";
$txtEmail_RegularExpressionValidator = $myform_manager->AddControl(new KoolRegularExpressionValidator("txtEmail_RegularExpressionValidator"));
$txtEmail_RegularExpressionValidator->TargetId = "txtEmail";
$txtEmail_RegularExpressionValidator->Expression = "/^[\w\.\-]+@[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]{1,})*(\.[a-zA-Z]{2,3}){1,2}$/";
$txtEmail_RegularExpressionValidator->ErrorMessage = "Please, enter valid e-mail address.";
$myform_manager->Validate = true; //Validate form before post
$myform_manager->RenderWithExistingMarkup = true; //Use existing page layout
$myform_manager->StatePersistent = false;//Don't keep state persistent after postback.
$myform_manager->Init();
?>
<form id="myform" method="post">
<fieldset style="width:600px;padding-left:5px;padding-bottom:5px;" class="decoration">
<legend>Please enter information</legend>
<table style="margin-top:5px;">
<tr>
<td style="height:25px;width:75px;">
Name:
</td>
<td>
<input id="txtName" name="txtName" type="text" />
<span id="txtName_RequiredFieldValidator"></span>
</td>
</tr>
<tr>
<td style="height:25px;">
Age:
</td>
<td>
<input id="txtAge" name="txtAge" type="text" />
<span id="txtAge_RequiredFieldValidator"></span>
<span id="txtAge_RangeValidator"></span>
</td>
</tr>
<tr>
<td style="height:25px;">
Phone:
</td>
<td>
<input id="txtPhone" name="txtPhone" type="text" />
<span id="txtPhone_RequiredFieldValidator"></span>
<span id="txtPhone_RegularExpressionValidator"></span>
</td>
</tr>
<tr>
<td style="height:25px;">
Email:
</td>
<td>
<input id="txtEmail" name="txtEmail" type="text" />
<span id="txtEmail_RequiredFieldValidator"></span>
<span id="txtEmail_RegularExpressionValidator"></span>
</td>
</tr>
</table>
<div style="margin-bottom:5px;margin-top:5px;">
<input type="submit" value="Submit">
</div>
<?php
if($myform_manager->IsPostBack)
{
echo "<div style='margin-bottom:5px;font-size:bold;'>Form is posted sucessfully!</div>";
}
?>
</fieldset>
<?php echo $myform_manager->Render();?>
</form>