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

Using the Form Validation Event

This example shows how to validate a field value using the form's On Validate event. In this case we have a Registration Record form in which the value of the Password field should be identical to the value of the Confirm field.

  1. Add the On Validate event to the form.
  2. Within the event, add the code below:

ASP

Function Registration_OnValidate(Sender) 

  If Registration.Password.Value <> Registration.Confirm.Value Then
       Registration.Errors.addError("The values of Password and Confirm fields do not match.")
  End if

End Function

PHP

function Registration_OnValidate(& $sender) {
global $Registration;

  if ($Registration->Password->GetValue() != $Registration->Confirm->GetValue()) {
     $Registration->Errors->addError("The values of Password and Confirm fields do not match.");
  }

}

Perl

sub Registration_OnValidate() {

  if ($Registration->{Password}->{Value} ne $Registration->{Confirm}->{Value}) {
    $Registration->{Errors}->addError("The values of Password and Confirm fields do not match.");
  }

}

ColdFusion

<!---Registration_OnValidate--->
  <CFIF fldPassword NEQ fldConfirm>
    <CFSET strErrRegistration=strErrRegistration & "<br>" & "The values of Password and Confirm fields do not match.">
  </CFIF>

VB.Net

'Registration_OnValidate

  If Password.Value <> Confirm.Value Then
    errors.Add("Password","The values of Password and Confirm fields do not match.")
  End if

C#

//Registration_OnValidate

  if ((Password.Value != null && Confirm.Value != null) && (Password.Value.ToString() != Confirm.Value.ToString() )) {
    errors.Add("Password","The values of Password and Confirm fields do not match.");
  }
  

Java

//Registration_OnValidate

  if ( e.getRecord().getControl("Password").getValue() != null && 
        ! e.getRecord().getControl("Password").getValue().equals(
        e.getRecord().getControl("Confirm").getValue())) {
    e.getRecord().addError("The values in fields Password and Confirm do not match.");
}
  

See Also:

On Validate event, How to Use Control Validation


On-line, printable versions and updates