IsValidMail: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Added example and made function safer)
Line 2: Line 2:
<lowercasetitle/>
<lowercasetitle/>
__NOTOC__
__NOTOC__
This function allows you to check if the provided e-mail string is theorically valid.


This function allows you to check e-mails, if they are in the correct format.
==Syntax==
<syntaxhighlight lang="lua">bool isValidMail ( string e-mail )</syntaxhighlight>


==Syntax==
===Arguments===
<syntaxhighlight lang="lua">bool isValidMail ( string mail )</syntaxhighlight>
*''e-mail'': a string containing the e-mail to check.


===Returns===
===Returns===
Returns ''true'' when the email was valid, ''false'' when the email was invalid
Returns ''true'' if the e-mail is valid, ''false'' if a string was not provided or if it's not an e-mail.


==Code==
==Code==
<section name="Clientside and Serverside Script" class="server" show="true">
<section name="Function source" class="both" show="true">
<syntaxhighlight lang="lua">function isValidMail(mail)  
<syntaxhighlight lang="lua">function isValidMail(mail)
    assert(type(mail) == "string", "Bad argument @ isValidMail [string expected, got " .. tostring(mail) .. "]")
    return mail:match("[A-Za-z0-9%.%%%+%-]+@[A-Za-z0-9%.%%%+%-]+%.%w%w%w?%w?") ~= nil
end</syntaxhighlight></section>
Author: SuperHomie


return mail:match("[A-Za-z0-9%.%%%+%-]+@[A-Za-z0-9%.%%%+%-]+%.%w%w%w?%w?") ~= nil
==Example==
<section name="Server" class="server" show="true">
end</syntaxhighlight>
The next example adds a /registerme command which rrgisters the player who types it, providing that the resource this example is executed in has sufficient ACL permissions.
 
<syntaxhighlight lang="lua">function registerUser(player, commandName, username, password, email)
Author: SuperHomie
    if username and password and email then
        if isGuestAccount(getPlayerAccount(player)) then
            if isValidMail(email) then
                local newAccount = addAccount(username, password)
                if newAccount then
                    setAccountData(newAccount, "email", email)
                    logIn(player, username, password)
                    outuputChatBox("Now you're logged in i  your new account.", player, 0, 255, 0)
                else
                    outputChatBox(#password > 30 and "The specified password is too long." or "That account already exists.", player, 255, 0, 0)
                end
            else
                outputChatBox("The e-mail provided is not valid!", player, 255, 0, 0)
            end
        else
            outputChatBox("You are already logged in.", player, 255, 0, 0)
        end
    else
        outputChatBox("Syntax: /" .. commandName .. " (username) (password) (email)", player, 255, 255, 255)
    end
end
addCommandHandler("registerme", registerUser)</syntaxhighlight></section>

Revision as of 13:26, 20 July 2014

This function allows you to check if the provided e-mail string is theorically valid.

Syntax

bool isValidMail ( string e-mail )

Arguments

  • e-mail: a string containing the e-mail to check.

Returns

Returns true if the e-mail is valid, false if a string was not provided or if it's not an e-mail.

Code

Click to collapse [-]
Function source
function isValidMail(mail)
    assert(type(mail) == "string", "Bad argument @ isValidMail [string expected, got " .. tostring(mail) .. "]")
    return mail:match("[A-Za-z0-9%.%%%+%-]+@[A-Za-z0-9%.%%%+%-]+%.%w%w%w?%w?") ~= nil
end

Author: SuperHomie

Example

Click to collapse [-]
Server

The next example adds a /registerme command which rrgisters the player who types it, providing that the resource this example is executed in has sufficient ACL permissions.

function registerUser(player, commandName, username, password, email)
    if username and password and email then
        if isGuestAccount(getPlayerAccount(player)) then
            if isValidMail(email) then
                local newAccount = addAccount(username, password)
                if newAccount then
                    setAccountData(newAccount, "email", email)
                    logIn(player, username, password)
                    outuputChatBox("Now you're logged in i  your new account.", player, 0, 255, 0)
                else
                    outputChatBox(#password > 30 and "The specified password is too long." or "That account already exists.", player, 255, 0, 0)
                end
            else
                outputChatBox("The e-mail provided is not valid!", player, 255, 0, 0)
            end
        else
            outputChatBox("You are already logged in.", player, 255, 0, 0)
        end
    else
        outputChatBox("Syntax: /" .. commandName .. " (username) (password) (email)", player, 255, 255, 255)
    end
end
addCommandHandler("registerme", registerUser)