Android Code Snippet Sharing the goodness: Form Input Validation

Hi

As my b4a apps at times deal with form input validation, I needed some subs that I could just run in my forms e.g. checking if a textbox is blank, confirming passwords etc, so I came up with this...

For the messagebox functions please refer to this post https://www.b4x.com/android/forum/threads/sharing-the-goodness-msgbox-things.58894/

1. Check if two textboxes values are the same, if same don't continue.

B4X:
' verify input of two textboxes
Sub IsSame(valueToCheck As String, valueToCheck1 As String, valueToCheckTitle As String) As Boolean
    valueToCheck = valueToCheck.Trim
    valueToCheck1 = valueToCheck1.Trim
    If valueToCheck = valueToCheck1 Then
        Dim msg As String
        msg = "The " & valueToCheckTitle & " must not be " & valueToCheck1 & ", please correct this!"
        Error("Input", msg)
        Return True
    Else
        Return False
    End If
End Sub

Usage:

B4X:
If IsSame(txt1.text,txt2.text, "Username") = True Then Return

2. Check if textbox value is blank and don't continue if blank

B4X:
Sub IsBlank(valueToCheck As String, title As String) As Boolean
    valueToCheck = valueToCheck.Trim
    If valueToCheck.Length = 0 Then
        Dim msg As String
        msg = "The " & title & " cannot be blank, please enter the " & title.ToLowerCase  & "!"
        Error(title,msg)
        Return True
    Else
        Return False
    End If
End Sub

Usage:

B4X:
If IsBlank(txt1.text, "Email") = True The  Return

3. Check if two text boxes are not the same, if true, don't continue.

B4X:
Sub DoNotMatch(valueToCheck As String, valueToCheck1 As String, valueToCheckTitle As String, valueToCheck1Title As String) As Boolean
    valueToCheck = valueToCheck.Trim
    valueToCheck1 = valueToCheck1.Trim
    If valueToCheck <> valueToCheck1 Then
        Dim msg As String
        msg = "The " & valueToCheckTitle & " and the " & valueToCheck1Title & " do not match, please correct this!"
        Error("Input", msg)
        Return True
    Else
        Return False
    End If
End Sub

Usage:

B4X:
If DoNotMatch(txt1.text,txt2.text,"Password", "Confirm Password") = True then Return

4. Check if text box value is a valid email, if not don't continue.

B4X:
Sub IsEmail(EmailAddress As String) As Boolean
    If EmailAddress.Length = 0 Then Return True
    Dim MatchEmail As Matcher = Regex.Matcher("^(?i)[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])$", EmailAddress)
    If MatchEmail.Find = True Then
        Return True
    Else
        Return False
    End If
End Sub

B4X:
Sub IsNotValidEmail(valueToCheck As String, title As String) As Boolean
    valueToCheck = valueToCheck.Trim
    If IsEmail(valueToCheck)= False Then
        Dim msg As String
        msg = "The " & title & " is not a valid email address, please check and correct the " & title.ToLowerCase  & "!"
        Error(title,msg)
        Return True
    Else
        Return False
    End If
End Sub

Usage:
B4X:
If IsNotValidEmail(txt1.text,"Email Address") = True Then Return
 
Top