Validating an input field

From Morfikwiki.com

Jump to: navigation, search

Contents

[edit] Introduction

It is often that you have a numeric field that requires input validation before it is submitted in a database.

There are two alternative ways of doing this in Morfik WebOS AppsBuilder:

  1. At the Form (or control) design-level
  2. In the Table design-level via the Validation Rule mechanism

You can use both any method but the implementation at the Form or Control levels gives you a better control on the feedback to the users when incorrect value is entered.

The following methods outline how you can implement numeric validation to ensure that the input value is between 10 and 100.

[edit] Field Validation at the Form or control level

In a Single form, where you edit table details, if you only wish to implement validation for a single control then you could do define an OnChange event for the particular TextEdit, but if you wish to do validation for a set of controls on the Form then it may be more effective to do it all at once at the Form level, in on OnBeforeSubmit event.

This code framework is an example of how you could implement numeric validation at the control level. It can be obviously modified for a Form-level event handler.

Procedure MyForm.MyTextEditChange(Event: TDOMEvent);
Var
    i : Integer;
Begin
   Try
      i := StrToInt(TextEditNumber1.Text);
      If (i<10) or (i>100) Then 
          ShowMessage('Please enter a number between 10 and 100.');
    Except
        // This will be invoked if they don't enter numbers.
        ShowMessage('Please enter a number between 10 and 100.');
        Exit;
    End;
End;

It may initially be intuitive to use the keyup and keydown event handlers of controls to assess the pressed key, however because people could type very fast, most browser implementations (including both Internet Explorer and Firefox) can not always keep up with firing up of the Keyup/down events, thus your validation may miss some unwanted characters. The OnBlur event handler of controls is the event which is fired up when the user switches focus by just clicking away.

This code framework is an example of how you could implement alphabetic validation at the control level.

Procedure Index.TextEdit1Blur(Event: TDOMEvent);
Var
    Temp : String;
    i    : Integer;    
Begin
    Temp := TextEdit1.Text; 
    i    := 1;

    While i <= Length(Temp) Do
    Begin
        if (Temp[i] < 'A') OR (Temp[i] > 'z') Then Delete(Temp,i,1)
                                              Else Inc(i);                    
    End;

    TextEdit1.Text := Temp;    
End;

[edit] Field Validation at the Table level

Alternatively at Table design level, you could place following

"MyField" BETWEEN 10 AND 100 

in the Validation rule for a field called MyField

Notes:

  • You must include the double quotes around the field name.
  • If an incorrect value is entered using this method, you will get a database engine error message which may not be clear to the user.

[edit] External links


[edit] See also

Personal tools