search
HomePHP FrameworkThinkPHPAn article explaining how thinkphp5 performs asynchronous email verification

thinkphp frameworkThe tutorial column will introduce to you how thinkphp5 uses bootstrapvalidator to asynchronously verify emails. I hope it will be helpful to friends in need!

An article explaining how thinkphp5 performs asynchronous email verification

TP5 uses bootstrapvalidator for asynchronous verification email

js verification

/**
 * Created by HONGXIN on 2017-10-23.
 */
$(function () {
    $('form').bootstrapValidator({

        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },

        live: 'disabled',//验证失败后,提交按钮仍然是可选状态

        fields: {
            email: {
                message: '用户名验证失败',//默认
                verbose: false,
                validators: {
                    notEmpty: {
                        message: '邮箱不能为空'
                    },
                    emailAddress: {
                        message: '邮箱地址格式有误'
                    },
                    remote: {
                        url: '/ajax_email',
                        message:"此邮箱已经注册",
                        type: "post",
                        dataType: 'json',
                        data: {
                            //默认传递的就是输入框的值
                        },
                        delay: 500,//延迟效果
                    },
                }
            },
            password: {
                validators: {
                    notEmpty: {
                        message: '邮箱地址不能为空'
                    },
                    stringLength: {
                        min: 6,
                        max: 18,
                        message: '用户名长度必须在6到18位之间'
                    },
                },
            },
            password2: {
                validators: {
                    notEmpty: {
                        message: '确认密码不能为空'
                    },
                    identical: {
                        field: 'password',
                        message: '两次密码必须一致'
                    }
                }
            },
            username:{
                validators: {
                    notEmpty: {
                        message: '用户名不能为空'
                    },
                    stringLength: {
                        min: 2,
                        max: 8,
                        message: '用户名长度必须在2到8位之间'
                    }
                }
            }

        }
    });
});

TP5 processing

    public function ajax_email(){
        //该message可以为空,它替换JS验证的message属性
       echo json_encode(['valid'=>false,'message'=>'验证码不正确']);

    }

js Several points to note when verifying

  • verbose: false, which means that js verification is legal before asynchronous background verification, which reduces server pressure
  • data: { }, the value of the input box is passed by default, so generally there is no need to write this attribute, or it can be empty

Notes on the background

  • Note that it is not return Instead, echo
  • returns json format {'valid':true[,'message':'Verification successful']}

The above is the detailed content of An article explaining how thinkphp5 performs asynchronous email verification. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.