FMXStringHandling (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example is a Multi-Device Application that shows how to handle strings in a FMX Form. It is a simple register framework that checks if the entered username and password are valid for registration.

To build and test this example, create a Multi-Device Application - C++, then add the next objects to the form:

  • 3 TEdit controls and name them usernameEdit, passwordEdit and rPasswordEdit.
  • 3 TLabel controls.
  • 2 TButton controls and name them clearButton and registerButton.

Code

Add the following code to the OnCreate event handler of the form:

// ---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender) {
	passwordEdit->Password = true;
	rPasswordEdit->Password = true;

	Label1->Text = "Username:";
	Label2->Text = "Password:";
	Label3->Text = "Re-enter password:";
}

At this moment the form should look like in the following image:

RegForm.png

Add the following code to the OnClick event handler of the clearButton:

// ---------------------------------------------------------------------------
void __fastcall TForm1::clearButtonClick(TObject *Sender) {
	usernameEdit->Text = "";
	passwordEdit->Text = "";
	rPasswordEdit->Text = "";
}


The example accepts an username with the length between 5 and 25 characters and that begins with a letter. An accepted password has between 6 and 32 characters and contains a digit, a lowercase letter, a capital letter and a special character(#,$,%,...). Also the password and the re-entered password must match.

The following piece of code contains two functions that are used to verify the username and the password strings:

// ---------------------------------------------------------------------------
bool __fastcall checkUserName(String username) {
	// check length
	if (username.Length() < 5 || username.Length() > 25) {
		ShowMessage("Username must be between 5 and 25 characters long");
		return false;
	}
	// verify if first letter is a character
	if (TCharacter::IsDigit(username[1])) {
		ShowMessage("Username must begin with a letter");
		return false;
	}

	return true;
}

// ---------------------------------------------------------------------------
bool __fastcall checkPassword(String password, String rPassword) {
	// check password length
	if (password.Length() < 6 || password.Length() > 32) {
		ShowMessage("Your password must be 6 to 32 characters long");
		return false;
	}

	// parse the password string to look for a letter uppercase,
	// a letter lowercase, a number and a special character
	bool uppercase = false, lowercase = false, number = false,
		specialChar = false;

	for (int i = 1; i <= password.Length(); i++) {
		if (TCharacter::IsDigit(password[i])) {
			number = true;
		}
		else if (TCharacter::IsLetter(password[i])) {
			if (TCharacter::IsUpper(password[i])) {
				uppercase = true;
			}
			if (TCharacter::IsLower(password[i])) {
				lowercase = true;
			}
		}
		if (TCharacter::IsSymbol(password[i]) || TCharacter::IsPunctuation
			(password[i]) || TCharacter::IsSeparator(password[i])) {
			specialChar = true;
		}
	}

	if (!uppercase) {
		ShowMessage("Password must contain an capital letter!");
		return false;
	}

	if (!lowercase) {
		ShowMessage("Password must contain a lowercase letter!");
		return false;
	}

	if (!number) {
		ShowMessage("Password must contain a number!");
		return false;
	}

	if (!specialChar) {
		ShowMessage("Password must contain a special character!");
		return false;
	}
	// check if the two password match

	if (password.Compare(rPassword)) {
		ShowMessage(
			"The passwords you entered do not match! Please try again!");
		return false;
	}

	return true;

And finally, and the following piece of code to the OnClick event handler of the registerButton:

// ---------------------------------------------------------------------------
void __fastcall TForm1::registerButtonClick(TObject *Sender) {
	System::String username;
	System::String password;
	System::String rPassword;

	username = usernameEdit->Text;
	password = passwordEdit->Text;
	rPassword = rPasswordEdit->Text;
	if (checkUserName(username) && checkPassword(password, rPassword)) {
		// add information to INI file
		ShowMessage("Success!");
	}
}

Uses

See Also