【问题标题】:Removing Gmail/Hotmail from Allowed Email Input从允许的电子邮件输入中删除 Gmail/Hotmail
【发布时间】:2020-09-15 09:14:39
【问题描述】:

我们公司目前在使用免费试用表格时遇到了问题。我们只希望合法的公司注册,目前有很多 gmail 帐户和 hotmail 帐户等利用试用然后创建新帐户然后基本上获得另一个免费试用。

这是 PHP 模块控制器中的电子邮件验证功能,只是想知道如何编辑它以不允许 gmail 帐户等,因为我已经跳入了这个已经设置的代码库。

protected $allowAnonymous = ['index', 'do-something', 'render-form', 'submit-marketplace', 'validate-email', 'verify-code', 'create-company'];

public function actionValidateEmail()
{
    $email = $_POST['email'];

    $emailValid = false;

    $baseUrl = $_SERVER["ourcompany_API_BASE_URL"]."/userByEmail/";
    $apiKey = $_SERVER["ourcompany_API_KEY"];
    $authHeader = $_SERVER["ourcompany_HEADER"];

    $client = new \GuzzleHttp\Client();
    try {
        $response = $client->request('GET', $baseUrl . $email, [
            'headers' => [
                'content-type' => 'application/vnd.ourcompany.message-v1+json',
                'accept' => 'application/vnd.ourcompany.message-v1+json',
                'authorization' => $authHeader,
                'x-api-key' => $apiKey,
            ]
        ]);
        $emailValid = !($response->getStatusCode() == 200);
    } catch (ClientException $e) {
        // ClientException raised for 400 level errors
        $emailValid = true;
    }

    return json_encode(array("valid" => $emailValid));
}

那么前端是这样的。

   <form style="display: none;" method="post" id="create-company-form" action="/">
        <input
          type="hidden"
          name="action"
          value="ourcompany-in-action-module/ourcompany-in-action/create-company"
        />
        <input id="companyName" type="text" name="companyName" value="" />
        <input id="firstName" type="text" name="firstName" value="" />
        <input id="lastName" type="text" name="lastName" value="" />
        <input id="email" type="text" name="email" value="" />
        <input id="mobile" type="text" name="mobile" value="" />
        <input id="country" type="text" name="country" value="" />
        <input id="timezone" type="text" name="timezone" value="" />
      </form>

      <form style="display: none;" method="post" id="validate-email-form" action="/">
        <input
          type="hidden"
          name="action"
          value="company-in-action-module/company-in-action/validate-email"
        />
        <input id="emailValidationField" type="email" name="email" value="" />
      </form>

任何帮助将不胜感激,感谢社区。​​p>

【问题讨论】:

    标签: php email email-validation craftcms


    【解决方案1】:

    您可以将RegExp 与脚本一起使用,以查看是否为gmail,然后在输入中创建错误。
    Js 解决方案:

    function Clear() {
    document.getElementById("error").innerHTML ='';
    }
    function CheckMail() {
        return RegExp(/([a-zA-Z0-9]+)([\.{1}])?([a-zA-Z0-9]+)\@gmail([\.])com/g);
    }
    function Validation(elem) {
    isGmail=CheckMail();
    var result = true;
    if(isGmail.test(elem)) {
            result=false;
            $('#error').html('Gmail is not allowed!');
        }else{
         $('#error').html(' Domain Allowed!');
        }
        
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <input id="test" type="email" onChange="Validation(this.value)" onClick="Clear()">
    
    <p id="error"></p>

    PHP 解决方案:

    function Test($value) {
        $notallowed=array(
          'gmail.com', 'gmail.it'
        );
         // Split the email address at the @ symbol
        $email_parts = explode( '@', $value );
    
        // Pop off everything after the @ symbol
        $domain = array_pop( $email_parts );
        if ( in_array( $domain, $notallowed ) ) {
          return false;
        }
    
          return true; 
    
    
    }
    if(Test('test@lol.com')==FALSE){
     echo 'Not allowed';   
    }else{
    
     echo 'Allowed';   
    }
    

    你会根据你的情况调整它。

    【讨论】:

      猜你喜欢
      • 2017-05-11
      • 2022-07-17
      • 2011-03-02
      • 2014-11-30
      • 2010-11-19
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 2011-10-09
      相关资源
      最近更新 更多