IsValidMail: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (typo fixes; better formatting; small changes to text)
m (typo)
Line 2: Line 2:
<lowercasetitle/>
<lowercasetitle/>
__NOTOC__
__NOTOC__
This function allows you to check if a given string is a theoretically valid e-mail address.
This function allows you to check if a given string is theoretically a valid e-mail address.


==Syntax==
==Syntax==

Revision as of 17:35, 20 July 2014

This function allows you to check if a given string is theoretically a valid e-mail address.

Syntax

bool isValidMail ( string mail )

Arguments

  • mail: A string containing an e-mail address

Returns

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

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 getElementType( player ) == "player" then
        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, newAccount, 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
end
addCommandHandler( "registerme", registerUser )