Home

String-Based Application: Password Validation

   

Introduction

This application is a dialog box that checks that an employee is following some rules when creating a password.

As the user is typing a password, the application counts the number of lowercase characters, the number of uppercase letters, the number of digits, and the number of symbols (special characters):

open System
open System.Windows.Forms

let exercise = new Form(Width = 385, Height = 240, Text = "Create Passrod")

// New Password
exercise.Controls.Add(new Label(Left = 12, Top = 22, Width = 85, Text = "New Password:"))
let txtNewPassword = new TextBox(Left = 112, Top = 19, Width = 124)
exercise.Controls.Add txtNewPassword

// Password Length
let txtPasswordLength = new TextBox(Left = 242, Top = 19, Width = 20)
exercise.Controls.Add txtPasswordLength
exercise.Controls.Add(new Label(Left = 267, Top = 22, Width = 80, Text = "Characters"))

// Confirm Password
exercise.Controls.Add(new Label(Left = 12, Top = 51, Width = 100, Text = "Confirm Password:"))
let txtConfirmPassword = new TextBox(Left = 112, Top = 48, Width = 124)
exercise.Controls.Add txtConfirmPassword

// Lowercase Letters
let txtLowercaseLetters = new TextBox(Left = 112, Top = 78, Width = 20)
exercise.Controls.Add txtLowercaseLetters
exercise.Controls.Add(new Label(Left = 136, Top = 81, Width = 100, Text = "Lowercase Letters"))

// Uppercase Letters
let txtUppercaseLetters = new TextBox(Left = 242, Top = 78, Width = 20)
exercise.Controls.Add txtUppercaseLetters
exercise.Controls.Add(new Label(Left = 266, Top = 81, Width = 100, Text = "Uppercase Letters"))

// Digits
let txtDigits = new TextBox(Left = 112, Top = 108, Width = 20)
exercise.Controls.Add txtDigits
exercise.Controls.Add(new Label(Left = 136, Top = 111, Width = 100, Text = "Digits"))

// Symbols
let txtSymbols = new TextBox(Left = 242, Top = 111, Width = 20)
exercise.Controls.Add txtSymbols
exercise.Controls.Add(new Label(Left = 266, Top = 114, Width = 100, Text = "Symbols"))

let txtNewPasswordTextChanged e =
    let mutable digits = 0
    let mutable symbols = 0
    let mutable lowercaseLetters = 0
    let mutable uppercaseLetters = 0
    let length = txtNewPassword.Text.Length

    for i = 0 to length - 1 do
        if Char.IsDigit(txtNewPassword.Text.[i]) then
            digits <- digits + 1
        if Char.IsSymbol(txtNewPassword.Text.[i]) || Char.IsPunctuation(txtNewPassword.Text.[i]) then
            symbols <- symbols + 1
        if Char.IsLower(txtNewPassword.Text, i) then
            lowercaseLetters <- lowercaseLetters + 1
        if Char.IsUpper(txtNewPassword.Text, i) then
            uppercaseLetters <- uppercaseLetters + 1
    
    txtDigits.Text <- string digits
    txtSymbols.Text <- string symbols
    txtLowercaseLetters.Text <- string lowercaseLetters
    txtUppercaseLetters.Text <- string uppercaseLetters

    txtPasswordLength.Text <- string length
txtNewPassword.TextChanged.Add txtNewPasswordTextChanged

// Message
let lblMessage = new Label(Left = 109, Top = 182, Width = 200)
exercise.Controls.Add lblMessage

// Submit
let btnSubmit = new Button(Left = 112, Top = 146, Width = 75, Text = "Submit")
exercise.Controls.Add btnSubmit

let btnSubmitClick e =
    let strNewPassword = txtNewPassword.Text
    let strConfirmPassword = txtConfirmPassword.Text

    if strNewPassword.Equals(strConfirmPassword) = false then
        lblMessage.Text <- "The passwords are different"
    else
        lblMessage.Text <- ""

btnSubmit.Click.Add btnSubmitClick

// Close
let btnClose = new Button(Left = 193, Top = 146, Width = 75, Text = "Close")
exercise.Controls.Add btnClose

let btnCloseClick e = exercise.Close()
btnClose.Click.Add btnCloseClick

do Application.Run exercise

Here is an example of running the program:

Equality of Two Strings

Equality of Two Strings

Equality of Two Strings

 
     
 

Home Copyright © 2015, FunctionX Home