Archive for luglio 2022
Controllo dell’Iban
In questo articolo vedremo come verificare se un codice Iban è corretto.
E’ sempre bello dare al cliente un programmino personalizzato con il proprio nome o proprio logo, per evitare che poi lui vada a cercare su internet il controllo (e ci sono siti che lo fanno).
Il codice di verifica è molto semplice, vengono usate le espressioni regolari per verificare il formato mentre per la verifica del codice CIN viene usato il MOD 97, ossia la divisione per 97 e avere il resto.
Questo è il codice della funzione di verifica.
Public Function VALIDATEIBAN(ByVal IBAN As String) As String
Dim objRegExp As Object
Dim IBANformat As Boolean
Dim IBANNR As String
Dim ReplaceChr As String
Dim ReplaceBy As String
'Check format
IBAN = UCase(IBAN)
IBAN = Replace(IBAN, " ", "")
objRegExp = CreateObject("vbscript.regexp")
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}"
IBANformat = objRegExp.Test(IBAN)
'Validity of country code will not be checked!
If IBANformat = False Then
VALIDATEIBAN = "FORMAT NOT RECOGNIZED"
Else
'Flip first 4 characters to the back
IBANNR = Right(IBAN, Len(IBAN) - 4) & Left(IBAN, 4)
'Replace letters by the right numbers
For Nr = 10 To 35
ReplaceChr = Chr(Nr + 55)
ReplaceBy = Trim(Str(Nr))
IBANNR = Replace(IBANNR, ReplaceChr, ReplaceBy)
Next Nr
'Loop through the IBAN, as it is too long to calculate at one go
CurrPart = ""
Answer = ""
For CurrDigit = 1 To Len(IBANNR)
CurrPart = CurrPart & Mid$(IBANNR, CurrDigit, 1)
CurrNumber = CLng(CurrPart)
'If the number can be divided
If 97 <= CurrNumber Then
LeftOver = CurrNumber Mod 97
WorkValue = (CurrNumber - LeftOver) / 97
Answer = Answer & CStr(WorkValue)
CurrPart = CStr(LeftOver)
Else
'If no division occurred, add a trailing zero
If Len(Answer) > 0 Then
Answer = Answer & "0"
'Exception for the last number
If CurrDigit = Len(IBANNR) Then
LeftOver = CurrNumber Mod 97
Else
End If
Else
End If
End If
Next
If LeftOver = 1 Then
VALIDATEIBAN = "IBAN OK"
Else
VALIDATEIBAN = "IBAN NON OK"
End If
End If
Return VALIDATEIBAN
Exit Function
End Function
Private Function Left(iBAN As String, v As Integer) As String
Return Mid(iBAN, 1, v)
End Function
Private Function Right(iBAN As String, v As Integer) As String
Return Mid(iBAN, Len(iBAN) - v + 1, v)
End Function
Il codice non è mio, l’ho trovato su internet e poi ho fatto qualche personalizzazione.
Per lanciare la verifica, ho creato un pulsante e ci mettete questo codice:
Dim ibanStr, result As String
Dim i As Integer
Dim rr As Boolean
ibanStr = "IT03J0100501628000000000547"
result = VALIDATEIBAN(ibanStr)
If (result = "IBAN OK") Then
MsgBox("IBAN OK")
Else
MsgBox("IBAN NON OK")
End If
Ovviamente il codice Iban da verificare lo potete passare tramite una TextBox, o dati presi da un database, io per emplicità l’ho messo nel codice direttamente.
Fonte e crediti:
' Created by : Koen Rijnsent (www.castoro.nl)
' Inspired by : Chris Fannin (AbbydonKrafts)
' Inspired by : bonsvr (http://stackoverflow.com/users/872583/bonsvr)

Controllo complessità Password
Se vi serve un programmino per il controllo se la propria password è sicura o meno, ho trovato in un sito Microsoft il seguente codice che ho provato e funziona.
Ve lo consiglio.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim password As String = "Password"
' Demonstrate that "Password" is not complex.
MsgBox(password & " is complex: " & ValidatePassword(password))
password = "Z9f%a>2kQ"
' Demonstrate that "Z9f%a>2kQ" is complex.
MsgBox(password & " is complex: " & ValidatePassword(password))
End Sub
Function ValidatePassword(ByVal pwd As String,
Optional ByVal minLength As Integer = 8,
Optional ByVal numUpper As Integer = 2,
Optional ByVal numLower As Integer = 2,
Optional ByVal numNumbers As Integer = 2,
Optional ByVal numSpecial As Integer = 2) As Boolean
' Replace [A-Z] with \p{Lu}, to allow for Unicode uppercase letters.
Dim upper As New System.Text.RegularExpressions.Regex("[A-Z]")
Dim lower As New System.Text.RegularExpressions.Regex("[a-z]")
Dim number As New System.Text.RegularExpressions.Regex("[0-9]")
' Special is "none of the above".
Dim special As New System.Text.RegularExpressions.Regex("[^a-zA-Z0-9]")
' Check the length.
If Len(pwd) < minLength Then Return False
' Check for minimum number of occurrences.
If upper.Matches(pwd).Count < numUpper Then Return False
If lower.Matches(pwd).Count < numLower Then Return False
If number.Matches(pwd).Count < numNumbers Then Return False
If special.Matches(pwd).Count < numSpecial Then Return False
' Passed all checks.
Return True
End Function


Risoluzione di un triangolo rettangolo
Quando andavo a scuola, ormai ben 35 anni fa circa, una delle materie che mi piacevano di più era la matematica. Poi quando spiegarono la trigonometria, mi si aprì un mondo e oggi matematica a trigonometria sono delle mie passioni.
Cercando delle informazioni per un altro articolo, mi sono trovato su alcuni siti che parlavano dell’acronimo SohCahToa. Non l’avevo mai sentito e così ho indagato e da questo nasce il seguente articolo.
Iniziamo av edere cosa è e cosa significa questo acronimo. dividiamolo in 3 acronimi da tre lettere.
Otteniamo: Soh, Cah e Toa. Le prime lettere di ognuna delle tre parole indicano seno, coseno e tangente.
La o indica il lato opposto all’angolo Teta, la a indica l’adiacente all’angolo Teta e la h indica l’ipotenusa.
Parliamo ovviamente di un triangolo rettangolo e questi acronimi servono a trovare i dati mancanti del triangolo, conoscendone 2. Soh significa Sin=Opposto diviso ipotenusa, Cah significa Cos=adiacente diviso ipotenusa e Toa significa Tangente =Opposto diviso adiacente.
La figura di seguito mostra la situazione:

Creiamo quindi un piccolo programmino per il calcolo dei dati mancanti del triangolo rettangolo, conoscendone solo 2. Apriamo Visual Studio e creiamo un progetto Windows Forms in Vb.NET.
Ci mettiamo 5 label, 4 textbox e 1 pulsante. Ho messo poi nel form le immagini per aiutarci.
Questa è la Form:

La prima cosa da fare è verificare che siano riempiti 2 campi:
If txtIpotenusa.Text = "" Then vuoto = vuoto + 1
If txtCateto1.Text = "" Then vuoto = vuoto + 1
If txtCateto2.Text = "" Then vuoto = vuoto + 1
If txtAngoloTeta.Text = "" Then vuoto = vuoto + 1
If vuoto > 2 Then MsgBox("Attenzione, solo 2 campi possono essere vuoti") : Exit Sub
La seconda cosa da fare è verificare che i campi riempiti siano numerici:
If txtIpotenusa.Text <> "" And IsNumeric(txtIpotenusa.Text) = False Then MsgBox("Attenzione, l'ipotenusa deve essere un numero") : Exit Sub
If txtCateto1.Text <> "" And IsNumeric(txtCateto1.Text) = False Then MsgBox("Attenzione, cateto1 deve essere un numero") : Exit Sub
If txtCateto2.Text <> "" And IsNumeric(txtCateto2.Text) = False Then MsgBox("Attenzione, cateto2 deve essere un numero") : Exit Sub
If txtAngoloTeta.Text <> "" And IsNumeric(txtAngoloTeta.Text) = False Then MsgBox("Attenzione, Teta deve essere un numero") : Exit Sub
Vediamo adesso caso per caso cosa c’è e quindi cosa manca:
If txtIpotenusa.Text = "" And (txtCateto1.Text <> "" And txtCateto2.Text <> "") Then manca = "I"
If txtCateto1.Text = "" And (txtIpotenusa.Text <> "" And txtCateto2.Text <> "") Then manca = "O"
If txtCateto2.Text = "" And (txtCateto1.Text <> "" And txtIpotenusa.Text <> "") Then manca = "A"
If txtAngoloTeta.Text <> "" And txtCateto1.Text <> "" Then manca = "T1"
If txtAngoloTeta.Text <> "" And txtCateto2.Text <> "" Then manca = "T2"
If txtAngoloTeta.Text <> "" And txtIpotenusa.Text <> "" Then manca = "T3"
E vediamo infine caso per caso quale formula delle tr3 richiamare, in base a quello che conosciamo.
Mettiamo assieme il tutto e questo è il codice finale:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim manca As String = ""
Dim Opposto As Double
Dim Adiacente As Double
Dim Ipotenusa As Double
Dim SinTeta As Double
Dim CosTeta As Double
Dim TanTeta As Double
Dim Teta As Double
Dim Teta1 As Double
Dim Teta2 As Double
Dim Teta3 As Double
Dim vuoto = 0
If txtIpotenusa.Text = "" Then vuoto = vuoto + 1
If txtCateto1.Text = "" Then vuoto = vuoto + 1
If txtCateto2.Text = "" Then vuoto = vuoto + 1
If txtAngoloTeta.Text = "" Then vuoto = vuoto + 1
If vuoto > 2 Then MsgBox("Attenzione, solo 2 campi possono essere vuoti") : Exit Sub
If txtIpotenusa.Text <> "" And IsNumeric(txtIpotenusa.Text) = False Then MsgBox("Attenzione, l'ipotenusa deve essere un numero") : Exit Sub
If txtCateto1.Text <> "" And IsNumeric(txtCateto1.Text) = False Then MsgBox("Attenzione, cateto1 deve essere un numero") : Exit Sub
If txtCateto2.Text <> "" And IsNumeric(txtCateto2.Text) = False Then MsgBox("Attenzione, cateto2 deve essere un numero") : Exit Sub
If txtAngoloTeta.Text <> "" And IsNumeric(txtAngoloTeta.Text) = False Then MsgBox("Attenzione, Teta deve essere un numero") : Exit Sub
If txtIpotenusa.Text = "" And (txtCateto1.Text <> "" And txtCateto2.Text <> "") Then manca = "I"
If txtCateto1.Text = "" And (txtIpotenusa.Text <> "" And txtCateto2.Text <> "") Then manca = "O"
If txtCateto2.Text = "" And (txtCateto1.Text <> "" And txtIpotenusa.Text <> "") Then manca = "A"
If txtAngoloTeta.Text <> "" And txtCateto1.Text <> "" Then manca = "T1"
If txtAngoloTeta.Text <> "" And txtCateto2.Text <> "" Then manca = "T2"
If txtAngoloTeta.Text <> "" And txtIpotenusa.Text <> "" Then manca = "T3"
If txtIpotenusa.Text <> "" Then Ipotenusa = CDbl(txtIpotenusa.Text)
If txtCateto1.Text <> "" Then Opposto = CDbl(txtCateto1.Text)
If txtCateto2.Text <> "" Then Adiacente = CDbl(txtCateto2.Text)
If txtAngoloTeta.Text <> "" Then Teta = CDbl(txtAngoloTeta.Text)
'CALCOLA ELEMENTI MANCANTI
If manca <> "T1" And manca <> "T2" And manca <> "T3" And manca <> "T4" Then
If manca = "I" Then
Ipotenusa = Math.Sqrt(Opposto ^ 2 + Adiacente ^ 2)
txtIpotenusa.Text = Ipotenusa
End If
If manca = "O" Then
Opposto = Math.Sqrt(Ipotenusa ^ 2 - Adiacente ^ 2)
txtCateto1.Text = Opposto
End If
If manca = "A" Then
Adiacente = Math.Sqrt(Ipotenusa ^ 2 - Opposto ^ 2)
txtCateto2.Text = Adiacente
End If
SinTeta = Opposto / Ipotenusa
CosTeta = Adiacente / Ipotenusa
TanTeta = Opposto / Adiacente
Teta1 = Math.Asin(SinTeta) * 180 / Math.PI
Teta2 = Math.Asin(SinTeta) * 180 / Math.PI
Teta3 = Math.Asin(SinTeta) * 180 / Math.PI
txtAngoloTeta.Text = Teta1 & " " & Teta2 & " " & Teta3
Else
SinTeta = Math.Sin(Teta * Math.PI / 180)
CosTeta = Math.Cos(Teta * Math.PI / 180)
TanTeta = Math.Tan(Teta * Math.PI / 180)
If manca = "T1" Then
'If txtAngoloTeta.Text <> "" And txtCateto1.Text <> "" Then manca = "T1"
'CALCOLARE IPOTENUSA E ADIACENTE
Ipotenusa = Opposto / SinTeta
Adiacente = Opposto / TanTeta
txtIpotenusa.Text = Ipotenusa
txtCateto2.Text = Adiacente
ElseIf manca = "T2" Then
'If txtAngoloTeta.Text <> "" And txtCateto2.Text <> "" Then manca = "T2"
'CALCOLARE IPOTENUSA E OPPOSTO
Ipotenusa = Adiacente / CosTeta
Opposto = TanTeta * Adiacente
txtIpotenusa.Text = Ipotenusa
txtCateto1.Text = Opposto
Else
'If txtAngoloTeta.Text <> "" And txtIpotenusa.Text <> "" Then manca = "T3"
'CALCOLARE ADIACENTE E OPPOSTO
Adiacente = CosTeta * Ipotenusa
Opposto = TanTeta * Adiacente
txtCateto2.Text = Adiacente
txtCateto1.Text = Opposto
End If
End If
End Sub
End Class
Notate che ho scritto 3 volte nella casella dell’angolo, il suo valore, perchè l’arco il cui sento, l’arco il cui coseno e l’arco la cui tangente devono dare 3 valori esattamente uguali, quindi li ho scritto per conferma dell’esattezza dei nostri calcoli. Ovviamente voi potete cambiare e mettere il valore una sola volt, o su tre Textbox diverse.
Vi metto infine qualche esempio da provare:




Ovviamente, ma non l’ho scritto nel codice, si può calcolare l’angolo fra l’ipotenusa e il lato opposto, facendo 90- l’angolo teta, poichè la somma degli angoli è 180 gradi ed essendo triangolo rettangolo, il valore dell’angolo fra i due cateti è sempre 90.
Leggi l'articolo intero | Make a Comment ( None so far )