B4J Question [SOLVED][ABMaterial] ABMInput - How to check if text is valid email

Xandoca

Active Member
Licensed User
Longtime User
Hi,

How can i check if ABMInput is valid?
I'm using an ABMInput to get the user email and I want to check if the email is valid before proceed (button click event).

B4X:
    Public inp2 As ABMInput
    inp2.Initialize(page,"inputemail",ABM.INPUT_EMAIL,"",False,"input")
    '...
    
    Private inp2 As ABMInput = cont.Component("inputemail")
    Log(inp2.SuccessMessage) 'does not work
    Log(inp2.Validate) 'return true
    Log(inp2.Valid) 'return empty
    If Not (inp2.Valid) Then
        page.ShowToast("toast2","","Pls fill an valid email address",2000,False)
        Return
    End If
The above code but it's not working.

Thanks.
Best Regards.
 

alwaysbusy

Expert
Licensed User
Longtime User
This may require some explanation indeed. ABM is only the UI part, so as for the logic, we need to write this ourselves to check if it is a valid email.

.Validate: do we check if it is valid (default is true)
.SuccessMessage: the message to show under the input filed if it is valid
.WrongMessage: the message to show under the input when invalid
.Valid: is a 3-state value (ABM.VALID_TRUE, ABM.VALID_FALSE, ABM.VALID_UNDETERMINED)

if ABM.VALID_TRUE then the SuccessMessage is shown
if ABM.VALID_FALSE then the WrongMessage is shown
if ABM.VALID_UNDETERMINED then neither one of the messages is shown

Example:
B4X:
...
Dim inp5 As ABMInput
inp5.Initialize(page, "inp5", ABM.INPUT_EMAIL, "Email", False, "input")    
inp5.Validate = True ' is the default, but just for the sake of the example
inp5.SuccessMessage = "Valid email"
inp5.WrongMessage = "Invalid email"
page.Cell(5,1).AddComponent(inp5)
...

Sub btn1_Clicked(Target As String)    
    If Not(CheckEmail) Then
        Log("Woops, no valid (or empty) email field!")
        Return
    End If
    
    ' input is ok, do something
    ' ...
    
    ' When clearing the field (for e.g. next input), we have to reset the .Valid
    Dim inp5 As ABMInput = page.Component("inp5")
    inp5.Text = ""
    inp5.Valid = ABM.VALID_UNDETERMINED
    inp5.Refresh
End Sub

Sub inp5_LostFocus()
    ' when the input lost focus, let's do a check to set the correct/wrong message    
    CheckEmail
End Sub

' checking the email field and setting the message in de browser accordingly
Sub CheckEmail() As Boolean
    Dim MayContinue As Boolean = True
    Dim inp5 As ABMInput = page.Component("inp5")
    If IsEmail(inp5.Text) Then
        inp5.Valid = ABM.VALID_TRUE        
    Else
        inp5.Valid = ABM.VALID_FALSE        
        MayContinue = False
    End If    
    inp5.Refresh
    Return MayContinue
End Sub

' Is this a valid email?
Sub IsEmail(EmailAddress As String) As Boolean
    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

Hope this helps

Alwaysbusy
 
Upvote 1

Harris

Expert
Licensed User
Longtime User
ABM.INPUT_EMAIL - the ABM way of testing an email address (text input contains an "@" symbol)..

Dim inp3 As ABMInput
inp3.Initialize(page, "inp3", ABM.INPUT_EMAIL, "Verify Email Address", False, "")
inp3.IconName = "mdi-communication-email"
inp3.WrongMessage = "Wrong format for email"
' inp3.SuccessMessage = "Correct"


However, this is no way to ensure that the email address provided is a "VALID" (reachable) email address.
There are on-line "services" that ping the email address to ensure that it is valid (a real email address).
This maybe helpful if you don't want to pollute your DB table with users / potential clients (or bots) by signing up for a free trial of your app - and never going further...

I implemented a unique confirmation registration code sent to the email address provided before a user can proceed (provide the reg-code upon first login). If they don't respond (email address unreachable), I delete the entry after so many days - garbage collection - clean the table.

There are many way to skin this cat - choose your sharpest weapon...
 
Upvote 0

Xandoca

Active Member
Licensed User
Longtime User
This may require some explanation indeed. ABM is only the UI part, so as for the logic, we need to write this ourselves to check if it is a valid email.

.Validate: do we check if it is valid (default is true)
.SuccessMessage: the message to show under the input filed if it is valid
.WrongMessage: the message to show under the input when invalid
.Valid: is a 3-state value (ABM.VALID_TRUE, ABM.VALID_FALSE, ABM.VALID_UNDETERMINED)

if ABM.VALID_TRUE then the SuccessMessage is shown
if ABM.VALID_FALSE then the WrongMessage is shown
if ABM.VALID_UNDETERMINED then neither one of the messages is shown

Example:
B4X:
...
Dim inp5 As ABMInput
inp5.Initialize(page, "inp5", ABM.INPUT_EMAIL, "Email", False, "input")   
inp5.Validate = True ' is the default, but just for the sake of the example
inp5.SuccessMessage = "Valid email"
inp5.WrongMessage = "Invalid email"
page.Cell(5,1).AddComponent(inp5)
...

Sub btn1_Clicked(Target As String)   
    If Not(CheckEmail) Then
        Log("Woops, no valid (or empty) email field!")
        Return
    End If
   
    ' input is ok, do something
    ' ...
   
    ' When clearing the field (for e.g. next input), we have to reset the .Valid
    Dim inp5 As ABMInput = page.Component("inp5")
    inp5.Text = ""
    inp5.Valid = ABM.VALID_UNDETERMINED
    inp5.Refresh
End Sub

Sub inp5_LostFocus()
    ' when the input lost focus, let's do a check to set the correct/wrong message   
    CheckEmail
End Sub

' checking the email field and setting the message in de browser accordingly
Sub CheckEmail() As Boolean
    Dim MayContinue As Boolean = True
    Dim inp5 As ABMInput = page.Component("inp5")
    If IsEmail(inp5.Text) Then
        inp5.Valid = ABM.VALID_TRUE       
    Else
        inp5.Valid = ABM.VALID_FALSE       
        MayContinue = False
    End If   
    inp5.Refresh
    Return MayContinue
End Sub

' Is this a valid email?
Sub IsEmail(EmailAddress As String) As Boolean
    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

Hope this helps

Alwaysbusy
Thank you very much! I'm going to use your code.
 
Upvote 0

Xandoca

Active Member
Licensed User
Longtime User
ABM.INPUT_EMAIL - the ABM way of testing an email address (text input contains an "@" symbol)..

Dim inp3 As ABMInput
inp3.Initialize(page, "inp3", ABM.INPUT_EMAIL, "Verify Email Address", False, "")
inp3.IconName = "mdi-communication-email"
inp3.WrongMessage = "Wrong format for email"
' inp3.SuccessMessage = "Correct"


However, this is no way to ensure that the email address provided is a "VALID" (reachable) email address.
There are on-line "services" that ping the email address to ensure that it is valid (a real email address).
This maybe helpful if you don't want to pollute your DB table with users / potential clients (or bots) by signing up for a free trial of your app - and never going further...

I implemented a unique confirmation registration code sent to the email address provided before a user can proceed (provide the reg-code upon first login). If they don't respond (email address unreachable), I delete the entry after so many days - garbage collection - clean the table.

There are many way to skin this cat - choose your sharpest weapon...
Thank you Harris. BTW thank you for your beginner ABM tutorial. It's help me a lot. ?

I think about implementing this confirmation code. Please, could you share your code? I understand if it is not possible ;)

Best regards!
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
Thank you Harris. BTW thank you for your beginner ABM tutorial. It's help me a lot.
Your welcome...
However, all of the credit must go to @alwaysbusy , since he re-created this wonderful add-on.
My minor part was only to help others get started with this amazing creation, since it helped me so much...

I can share my confirmation code. Allow me some time to post it since I am working on other projects - and most important - planting my veggie garden...
A man got's to eat...

In the mean time, carry on with your project.
I often get stuck with an issue - when there is SO much else to do - that I can back at later... (my current state).

Thanks
 
Upvote 0

Xandoca

Active Member
Licensed User
Longtime User
Your welcome...
However, all of the credit must go to @alwaysbusy , since he re-created this wonderful add-on.
My minor part was only to help others get started with this amazing creation, since it helped me so much...

I can share my confirmation code. Allow me some time to post it since I am working on other projects - and most important - planting my veggie garden...
A man got's to eat...

In the mean time, carry on with your project.
I often get stuck with an issue - when there is SO much else to do - that I can back at later... (my current state).

Thanks
Thank you and good luck with your veggie garden. It's a nice initiative.
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
Here ya go... More the concept than the code...

This form allows a client to signup for a trial.
The user must fill in all fields of the form with email address twice - and the password twice...

The CHECK USERNAME method ensures the user doesn't already exist.
When successful, the SUBMIT FORM button is enabled...

mms.png


This event submits the form and sends a confirmation email to the user.
DateTime.Now is used as a registration code for this user.
One field is set to "0". This is used to test if the user has already registered - or not.


B4X:
Sub btnsubmit_clicked(target As String)
   
Dim email As ABMInput = page.Component("inp5")

If email.Text = "" Then
    Toast("Email Address can not be empty!",5000)
    Return  
End If

page.Pause

Dim comp As ABMInput = page.Component("inp1")
Dim titl As ABMInput = page.Component("inp2")
Dim last As ABMInput = page.Component("inp4")

Dim phone As ABMInput = page.Component("inp6")
Dim loc As ABMInput = page.Component("inp7")
Dim usr As ABMInput = page.Component("inp9")
Dim pss As ABMInput = page.Component("inp10")

' this becomes the registration code - the current datetime number (do whatever you want)
Dim dat As Long = DateTime.Now  

Dim contmap As Map
contmap.Initialize

contmap.Put("comp", comp.Text)
contmap.Put("email", email.Text)
contmap.Put("loc", loc.Text)
contmap.Put("last", last.Text)
contmap.Put("usr", usr.Text)
contmap.Put("phone", phone.Text)
contmap.Put("pss", pss.Text)
contmap.Put("title", titl.Text)
contmap.Put("dat", dat)

   
Dim SQL As SQL = DBM.GetSQL

Dim vars As List
vars.Initialize
vars.Add(email.Text)
vars.Add(comp.Text)

Dim eml As List = DBM.SQLSelect(SQL, "SELECT * FROM member WHERE email = ? and company = ?", vars)
If eml.Size > 0 Then
    DBM.CloseSQL(SQL)
    page.Msgbox("ERR","","Email Address and Municipality Name Already Exist!"&CRLF&"Only ONE Subcription Per Subscriber Is Allowed! ","OK",False,ABM.MSGBOX_POS_CENTER_CENTER,"")
'    email.Text = ""
    email.Refresh
    page.Resume
    Return

End If
   
Toast("Waiting for email server response...",7000)
   
   
SQL.ExecNonQuery2("INSERT INTO member (company, title,first,last,location,email,phone,comment,date,notes,username,pass) VALUES(?, ?, ?, ?, ?, ?, ?, ?,?,?,?,?)", Array As Object(comp.text,titl.text,"",last.Text,loc.Text,email.Text,phone.Text,"",dat,"0",usr.text,pss.text ))    

DBM.CloseSQL(SQL)

email.Text = ""
email.Refresh
SendMail(contmap) ' send mail to email address...
   
End Sub

Example of email received by user...

B4X:
Welcome to Municipal Management System !

  Your FIRST login will require THIS Registration Number to confirm your subscription!
  Registration Number (copy and paste it):  1586649887677

Your Administrator Login is:
   User Name: margs
    Password: marg11

OTHER FORM DATA:

Municipality:   - RM of Estevan
Full Name:   - Marg Stevees
Title:   - Admin
Email:   - the_email_address@gmail.com
Phone #:   - 234
Mailing Address:
123 Main Street
Estevan, SK
N7N 1V1

Thank you for Subscribing to Municipal Management System.
Created By: Modern Mobile Solutions LLC.


When this new user attempts the first login, the Provide Registration Number becomes visible.
They copy and paste the reg code - and press login again...
If the number matches the DB table value, the table field is updated so the user is validated - and never asked to provide code again on future login attempts.
The user is then presented with the About Page of the private area of the web app.

1587579166705.png


Hope this helps....

Thanks
 
Last edited:
Upvote 0

Xandoca

Active Member
Licensed User
Longtime User
Here ya go... More the concept than the code...

This form allows a client to signup for a trial.
The user must fill in all fields of the form with email address twice - and the password twice...

The CHECK USERNAME method ensures the user doesn't already exist.
When successful, the SUBMIT FORM button is enabled...

View attachment 92396

This event submits the form and sends a confirmation email to the user.
DateTime.Now is used as a registration code for this user.
One field is set to "0". This is used to test if the user has already registered - or not.


B4X:
Sub btnsubmit_clicked(target As String)
   
Dim email As ABMInput = page.Component("inp5")

If email.Text = "" Then
    Toast("Email Address can not be empty!",5000)
    Return  
End If

page.Pause

Dim comp As ABMInput = page.Component("inp1")
Dim titl As ABMInput = page.Component("inp2")
Dim last As ABMInput = page.Component("inp4")

Dim phone As ABMInput = page.Component("inp6")
Dim loc As ABMInput = page.Component("inp7")
Dim usr As ABMInput = page.Component("inp9")
Dim pss As ABMInput = page.Component("inp10")

' this becomes the registration code - the current datetime number (do whatever you want)
Dim dat As Long = DateTime.Now  

Dim contmap As Map
contmap.Initialize

contmap.Put("comp", comp.Text)
contmap.Put("email", email.Text)
contmap.Put("loc", loc.Text)
contmap.Put("last", last.Text)
contmap.Put("usr", usr.Text)
contmap.Put("phone", phone.Text)
contmap.Put("pss", pss.Text)
contmap.Put("title", titl.Text)
contmap.Put("dat", dat)

   
Dim SQL As SQL = DBM.GetSQL

Dim vars As List
vars.Initialize
vars.Add(email.Text)
vars.Add(comp.Text)

Dim eml As List = DBM.SQLSelect(SQL, "SELECT * FROM member WHERE email = ? and company = ?", vars)
If eml.Size > 0 Then
    DBM.CloseSQL(SQL)
    page.Msgbox("ERR","","Email Address and Municipality Name Already Exist!"&CRLF&"Only ONE Subcription Per Subscriber Is Allowed! ","OK",False,ABM.MSGBOX_POS_CENTER_CENTER,"")
'    email.Text = ""
    email.Refresh
    page.Resume
    Return

End If
   
Toast("Waiting for email server response...",7000)
   
   
SQL.ExecNonQuery2("INSERT INTO member (company, title,first,last,location,email,phone,comment,date,notes,username,pass) VALUES(?, ?, ?, ?, ?, ?, ?, ?,?,?,?,?)", Array As Object(comp.text,titl.text,"",last.Text,loc.Text,email.Text,phone.Text,"",dat,"0",usr.text,pss.text ))    

DBM.CloseSQL(SQL)

Example of email received by user...

[CODE=b4x]

Welcome to Municipal Management System !

  Your FIRST login will require THIS Registration Number to confirm your subscription!
  Registration Number (copy and paste it):  1586649887677

Your Administrator Login is:
   User Name: margs
    Password: marg11

OTHER FORM DATA:

Municipality:   - RM of Estevan
Full Name:   - Marg Stevees
Title:   - Admin
Email:   - the_email_address@gmail.com
Phone #:   - 234
Mailing Address:
123 Main Street
Estevan, SK
N7N 1V1

Thank you for Subscribing to Municipal Management System.
Created By: Modern Mobile Solutions LLC.

email.Text = ""
email.Refresh
SendMail(contmap) ' send mail to email address...

End Sub
[/CODE]

When this new user attempts the first login, the Provide Registration Number becomes visible.
They copy and paste the reg code - and press login again...
If the number matches the DB table value, the table field is updated so the user is validated - and never asked to provide code again on future login attempts.
The user is then presented with the About Page of the private area of the web app.

View attachment 92399

Hope this helps....

Thanks

Thank you Harris, it's awesome. I've think about sending an url to user confirm/activate the account but it's a very good idea using this registration number at login (one time).

Regards.
Alexandre
 
Upvote 0
Top