IsValidMail: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (Typo at →‎Example)
m (typo fixes; better formatting; small changes to text)
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 if a given string is a theoretically valid e-mail address.


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">bool isValidMail ( string e-mail )</syntaxhighlight>
<syntaxhighlight lang="lua">bool isValidMail ( string mail )</syntaxhighlight>


===Arguments===
===Arguments===
*''e-mail'': a string containing the e-mail to check.
*'''mail''': A string containing an e-mail address


===Returns===
===Returns===
Returns ''true'' if the e-mail is valid, ''false'' if a string was not provided or if it's not an e-mail.
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==
==Code==
<section name="Function source" class="both" 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) .. "]")
     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
     return mail:match( "[A-Za-z0-9%.%%%+%-]+@[A-Za-z0-9%.%%%+%-]+%.%w%w%w?%w?" ) ~= nil
end</syntaxhighlight></section>
end</syntaxhighlight></section>
Author: SuperHomie
'''Author:''' SuperHomie


==Example==
==Example==
<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
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.
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)
<syntaxhighlight lang="lua">function registerUser( player, commandName, username, password, email )
     if username and password and email then
     if getElementType( player ) == "player" then
        if isGuestAccount(getPlayerAccount(player)) then
        if username and password and email then
            if isValidMail(email) then
            if isGuestAccount( getPlayerAccount( player ) ) then
                local newAccount = addAccount(username, password)
                if isValidMail( email ) then
                if newAccount then
                    local newAccount = addAccount( username, password )
                    setAccountData(newAccount, "email", email)
                    if newAccount then
                    logIn(player, newAccount, password)
                        setAccountData( newAccount, "email", email )
                    outputChatBox("Now you're logged in your new account.", player, 0, 255, 0)
                        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
                 else
                     outputChatBox(#password > 30 and "The specified password is too long." or "That account already exists.", player, 255, 0, 0)
                     outputChatBox( "The e-mail provided is not valid!", player, 255, 0, 0 )
                 end
                 end
             else
             else
                 outputChatBox("The e-mail provided is not valid!", player, 255, 0, 0)
                 outputChatBox( "You are already logged in.", player, 255, 0, 0 )
             end
             end
         else
         else
             outputChatBox("You are already logged in.", player, 255, 0, 0)
             outputChatBox( "Syntax: /" .. commandName .. " (username) (password) (email)", player, 255, 255, 255 )
         end
         end
    else
        outputChatBox("Syntax: /" .. commandName .. " (username) (password) (email)", player, 255, 255, 255)
     end
     end
end
end
addCommandHandler("registerme", registerUser)</syntaxhighlight></section>
addCommandHandler( "registerme", registerUser )</syntaxhighlight></section>

Revision as of 17:35, 20 July 2014

This function allows you to check if a given string is a theoretically 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 )