JavaScript uses the alert() function in jQuery $("ා). Blur() to cause infinite cycles to lose focus and execute the pop-up warning solution

Keywords: Front-end JQuery Mobile Javascript github

Problem analysis of this record:
1. Time difference problem:
When I click alert, I clear and focus the input box, but the moment I click alert, the focus is not on the input box, and the defocus event is triggered, resulting in the alert pop-up.
2. Sequence question:
With the mentality of first letting it pop up in the execution of clearing and focusing, I put alert on the clearing focus. However, whether alert is on the clearing focus or under the clearing focus, these codes are executed at the moment when you click OK after the alert pops up. In fact, the clear focus code has been read at the moment when the alert pops up, but the clear focus code needs to be executed at the moment when the alert pops up.

To sum up, alert is loaded first in js code, because the clear focus code is executed after the alert point is determined, but the focus is still on the alert pop-up window that is not fully closed at that moment, so the defocus event is triggered, resulting in infinite loop

For this reason, I found a lot of information on the Internet, which is still unsolved. At last, I used the whole method for reference with the help of my friends.


Use virtual pop-up window, and directly use zdalert() or zdconfirm() instead of alert.


<!DOCTYPE html>
<html lang="en">
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="./css/informationRegistration.css">
		<link rel="stylesheet" type="text/css" href="js/layer.js">
		<script src=""></script>
		<script type="text/javascript" src="./js/informationRegistration.js"></script>
		<script src="./js/jquery.min.js"></script>
		<script type="text/javascript" src="./js/jquery.form.js"></script>
		<script type="text/javascript" src="./js/virtualPop_up.js"></script>
		<script type="text/javascript" src="./js/dome.js"></script>

		<div class="zong">
			<div class="form">
				<form id="form2" action="##" onsubmit="return false" method="post" enctype="multipart/form-data">					
					<div class="changeDiv">
						<div class="ai">
							<label for="sgzh">ID number</label>
							<i class="icomoon">*</i>
						<input id="sgzh" type="text" name="idNumber" placeholder="Please enter ID number"
							   autocomplete="off"  maxlength="18" minlength="15"
							  ><!-- onKeyUp="value=value.replace(/[\W]/g)" -->
					<div class="changeDiv">
						<div class="ai">
							<label for="sjh">Cell-phone number</label>
							<i class="icomoon">*</i>
						<input type="text" id="sjh" placeholder="Please enter the owner's mobile number in pure numbers"
							   autocomplete="off"	maxlength=11 minlength="11"  >

. ulur() listens for html input focus events and calls the Virtual pop-up function js code:

function regular() {
	//ID card regular
	$(document).ready(function () {
		var _IDRe18 = /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
		var _IDre15 =  /^([1-6][1-9]|50)\d{4}\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}$/
		var ft=false;
		$("#sgzh").focus(function () {
		//Lose focus ID card regular
			var sgzhValue=document.getElementById("sgzh").value;//ID card No.
			// alert("the ID number you entered is:" + sgzhValue);
			var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
			if(reg.test(sgzhValue) === false)
				console.log('Get into.blur_sgzh')
				// alert("illegal ID card input");
				// confirm('message');
				zdalert("reminder!","The ID card number is wrong, please fill it in again!");

				return false;
	$(document).ready(function () {
		var ft=false;
		$("#sjh").focus(function () {
		//Lost focus phone number regular
		$("#sjh").blur(function () {
			var sjhValue = document.getElementById("sjh").value;//Cell-phone number
			// alert("mobile number you entered:" + sjhValue);
			var regphone = /^1(3|4|5|6|7|8|9)\d{9}$/;
			if (regphone.test(sjhValue)=== false) {

				console.log('Get into.blur_sgzh')
				// alert("mobile number is wrong or empty, please fill in again");
				// confirm('me');
				zdalert("reminder!","The mobile number is wrong, please fill it in again!");

				return false;

Virtual pop-up js source code: there are two methods with different functions to call

  (function($) {

                $.alerts = {
                    alert: function(title, message, callback) {
                        if(title == null) title = 'Alert';
                        $.alerts._show(title, message, null, 'alert', function(result) {
                            if(callback) callback(result);

                    confirm: function(title, message, callback) {
                        if(title == null) title = 'Confirm';
                        $.alerts._show(title, message, null, 'confirm', function(result) {
                            if(callback) callback(result);

                    _show: function(title, msg, value, type, callback) {

                        var _html = "";

                        _html += '<div id="mb_box"></div><div id="mb_con"><span id="mb_tit">' + title + '</span>';
                        _html += '<div id="mb_msg">' + msg + '</div><div id="mb_btnbox">';
                        if(type == "alert") {
                            _html += '<input id="mb_btn_ok" type="button" value="Determine" />';
                        if(type == "confirm") {
                            _html += '<input id="mb_btn_ok" type="button" value="Determine" />';
                            _html += '<input id="mb_btn_no" type="button" value="cancel" />';
                        _html += '</div></div>';

                        //You must add html to the body before setting the Css Style

                        switch(type) {
                            case 'alert':

                                $("#mb_btn_ok").click(function() {
                                $("#mb_btn_ok").focus().keypress(function(e) {
                                    if(e.keyCode == 13 || e.keyCode == 27) $("#mb_btn_ok").trigger('click');
                            case 'confirm':

                                $("#mb_btn_ok").click(function() {
                                    if(callback) callback(true);
                                $("#mb_btn_no").click(function() {
                                    if(callback) callback(false);
                                $("#mb_btn_ok, #mb_btn_no").keypress(function(e) {
                                    if(e.keyCode == 13) $("#mb_btn_ok").trigger('click');
                                    if(e.keyCode == 27) $("#mb_btn_no").trigger('click');

                    _hide: function() {
                // Shortuct functions
                zdalert = function(title, message, callback) {
                    $.alerts.alert(title, message, callback);

                zdconfirm = function(title, message, callback) {
                    $.alerts.confirm(title, message, callback);
                var GenerateCss = function() {

                        width: '100%',
                        height: '100%',
                        zIndex: '99999',
                        position: 'fixed',
                        filter: 'Alpha(opacity=60)',
                        backgroundColor: 'black',
                        top: '0',
                        left: '0',
                        opacity: '0.6'

                        zIndex: '999999',
                        width: '4.0rem',
                        position: 'fixed',
                        backgroundColor: 'White',
                        borderRadius: '3px'

                        display: 'block',
                        fontSize: '0.186667rem',
                        color: '#444',
                        padding: '0.133333rem 0.2rem',
                        backgroundColor: '#DDD',
                        borderRadius: '3px 3px 0 0',
                        fontWeight: 'bold'

                        padding: '0.266667rem',
                        lineHeight: '0.266667rem',
                        borderBottom: '1px dashed #DDD',
                        fontSize: ' 0.173333rem'

                        display: 'block',
                        position: 'absolute',
                        right: '0.133333rem',
                        top: '0.12rem',
                        border: '1px solid Gray',
                        width: '0.24rem',
                        height: '0.24rem',
                        textAlign: 'center',
                        lineHeight: '0.213333rem',
                        cursor: 'pointer',
                        borderRadius: '12px',
                        fontFamily: 'Microsoft YaHei'

                        margin: '0.133333rem 0 0.133333rem',
                        textAlign: 'center'
                        width: '1.133333rem',
                        height: '0.4rem',
                        color: 'white',
                        border: 'none'
                        backgroundColor: '#168bbb'
                        backgroundColor: 'gray',
                        // marginLeft: '0.266667rem'

                    //Top right close button hover style
                    $("#mb_ico").hover(function() {
                            backgroundColor: 'Red',
                            color: 'White'
                    }, function() {
                            backgroundColor: '#DDD',
                            color: 'black'

                    var _widht = document.documentElement.clientWidth; //Screen width
                    var _height = document.documentElement.clientHeight; //Screen height

                    var boxWidth = $("#mb_con").width();
                    var boxHeight = $("#mb_con").height();

                    //Center the prompt box
                        top: (_height - boxHeight) / 2 + "px",
                        left: (_widht - boxWidth) / 2 + "px"


The final printing effect is as follows:

The original design is to adapt to the mobile terminal, so the width size will be different in the PC presentation. You can modify CSS parameters in Virtual pop-up JS.

/*-------------------Anti crawler statement--------------------

Author: Yang Mu fa
Copyright notice:
This article is the original article of the blogger. Please attach the link of the source article for the whole reprint!

If you think this article has something to gain for you, please comment and comment on it

Reasonable and high-quality forwarding will also encourage and support me to continue to create,

For more highlights, Baidu can search for yangmufa or:

Personal website:    ,

Sky drive network:    ,

Open source China:    ,

Gitee:    ,

GitHub:    .

Insist on creation and be good at summarizing high-quality progress of open source sharing.

-------------------Anti crawler statement--------------------*/

Posted by BDabrowski on Wed, 26 Feb 2020 20:46:34 -0800