GF User Registration插件白名单邮箱注册验证的问题与解决
邮箱白名单验证:Gravity Forms 前端即时校验实现
我手里有一份包含约550个邮箱地址的白名单,需要让Gravity Forms的注册表单只接受这个列表里的邮箱,且适配任意域名。目前使用GF User Registration插件,之前试过社区提供的PHP验证代码,但无法拦截非白名单邮箱,因此希望实现表单提交前的JavaScript即时验证。
add_filter( 'gform_field_validation', function ( $result, $value, $form, $field ) { $whitelisted_emails = array( 'test@test.com', 'test2@test.com' ); if ( $result['is_valid'] && $field->get_input_type() === 'email' && ! in_array( $value, $whitelisted_emails ) ) { $result['is_valid'] = false; $result['message'] = '无效邮箱'; } return $result; }, 10, 4 );
更新:已通过jQuery实现该功能
jQuery(document).on('gform_post_render', function(){ // 每次表单加载(包括验证后)都执行 whitelistArray = new Array("test@test.com", "test2@test.com"); $('.gfield--type-email input').on('blur', function() { var emailValue = $(this).val(); $(this).parent().append('<div class="no-ticket">'+emailValue+' 不在我们的白名单内</div>'); $('.no-ticket:not(:last-child)').remove(); // 移除之前的提示信息 if( $.inArray(emailValue, whitelistArray) !== -1 ) { console.log("邮箱在白名单内"); $('.no-ticket').hide(); } else { $(this).val(''); $('.no-ticket').show(); } }); });
上述代码可适配所有Gravity Form表单,当邮箱输入框失焦时,校验输入邮箱是否在白名单数组内;若不在,则清空输入并显示提示信息「您输入的邮箱不在我们的白名单内」。由于邮箱为必填字段,空输入无法通过表单验证,字段描述已提示用户使用受邀邮箱注册。
内容的提问来源于stack exchange,提问作者pseudojk
相关产品推荐
相关产品推荐

