IsValidMail: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Added example and made function safer)
m (Typos at →‎Example)
Line 32: Line 32:
                     setAccountData(newAccount, "email", email)
                     setAccountData(newAccount, "email", email)
                     logIn(player, username, password)
                     logIn(player, username, password)
                     outuputChatBox("Now you're logged in your new account.", player, 0, 255, 0)
                     outputChatBox("Now you're logged in your new account.", player, 0, 255, 0)
                 else
                 else
                     outputChatBox(#password > 30 and "The specified password is too long." or "That account already exists.", player, 255, 0, 0)
                     outputChatBox(#password > 30 and "The specified password is too long." or "That account already exists.", player, 255, 0, 0)

Revision as of 13:28, 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)
                    outputChatBox("Now you're logged in 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)