CodeCharge Studio All ASP PHP Perl ColdFusion Java C#.NET VB.NET

Using the Control Validation Event

This example shows how to validate a field value using the control's On Validate Event. In this case, we have a form called Form1 which contains a Text Box called TextBox1. The value of Textbox1 should be greater than 10 and smaller than 50.

  1. Add an On Validate event to the control. Note that you can just as well add the On Validate event to the form instead of the control. Applying the On Validate event to the form enables you to validate several controls belonging to the form.
  2. Within the event, add the code below:

ASP

Function Form1_TextBox1_OnValidate(Sender)

  If Form1.TextBox1.Value < 10 or Form1.TextBox1.Value > 50 Then
    Form1.Errors.addError("The value must be greater than 10 and smaller than 50")
  End if

End Function

PHP

function Form1_TextBox1_OnValidate(& $sender) {
global $Form1;

  if ($Form1->TextBox1->GetValue() < 10 or $Form1->TextBox1->GetValue() > 50) {
    $Form1->Errors->addError("The value must be greater than 10 and smaller than 50");
  }

}

Perl

sub Form1_TextBox1_OnValidate() {

  if ($Form1->{TextBox1}->{Value} < 10 or $Form1->{TextBox1}->{Value} > 50) {
    $Form1->{Errors}->addError("The value must be greater than 10 and smaller than 50");
  }

}

ColdFusion

<!---Form1_TextBox1_OnValidate--->
  <CFIF fldTextBox1 LT 10 OR fldTextBox1 GT 50>
    <CFSET strErrRegistration=strErrRegistration & "The value must be greater than 10 and smaller than 50<br>">
  </CFIF>

VB.Net

'Form1_TextBox1_OnValidate

  If TextBox1.Value < 10 or TextBox1.Value > 50 Then
    errors.Add("TextBox1","The value must be greater than 10 and smaller than 50")
  End if

C#

Note: Select Integer in the Data Type property of the TextBox1 Text Box.

//Form1_TextBox1_OnValidate

  if (TextBox1.Value != null && ((Int64)TextBox1.Value < 10 || (Int64)TextBox1.Value > 50)) {
    errors.Add("TextBox1","The value must be greater than 10 and smaller than 50");
  }

Java

//Form1_TextBox1_OnValidate

  if ( e.getControl().getValue() != null &&
       (Utils.convertToLong(e.getControl().getValue()).longValue() < 10 ||
        Utils.convertToLong(e.getControl().getValue()).longValue() > 50) ) {
    e.getControl().addError("The value must be greater than 10 and smaller than 50");
  }
  

See Also:

On Validate event, How to use the Form Validation Event


On-line, printable versions and updates