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.
span style="color: #ff0000;">"/KoolAjax/koolajax.php""/KoolGrid/koolgrid.php""/KoolForm/koolform.php";
$koolajax->scriptFolder = $KoolControlsFolder."/KoolAjax""myform""/KoolForm""sunset";
//Use the numeric textbox for Age
"txtAge"));
//Use the masked textbox for phone
"txtPhone""(###)-######";
$txtPhone->SelectionOnFocus = "CaretToBeginning";
//Create the required field validator for txtName
"txtName_RequiredFieldValidator""txtName""The textbox can not be empty!";
//Create the required field validator and range validator for txtName
"txtAge_RequiredFieldValidator""txtAge""Please, select an year number!""txtAge_RangeValidator""txtAge""Year number should be a non negative less than 50."//Create the required field validator and regular expression validator for txtPhone
"txtPhone_RequiredFieldValidator""txtPhone""Please enter phone number!""txtPhone_RegularExpressionValidator""txtPhone""/\d{9}/""Format is (###)-######";
//Create the required field validator and regular expression validator for txtEmail
"txtEmail_RequiredFieldValidator""txtEmail""Please, enter an e-mail!""txtEmail_RegularExpressionValidator""txtEmail""/^[\w\.\-]+@[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]{1,})*(\.[a-zA-Z]{2,3}){1,2}$/""Please, enter valid e-mail address."//Validate form before post
//Use existing page layout
//Don't keep state persistent after postback.
"myform" method="post">
<fieldset style="width:600px;padding-left:5px;padding-bottom:5px;""decoration""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;""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 style='margin-bottom:5px;font-size:bold;'>Form is posted sucessfully!</div>"