Home > Article > Daily Programming > jQuery form verification submission: front-end verification 2 (image, text + video)
This article mainly introduces in detail the specific method of jQuery to implement form verification submission.
In the previous article [jQuery form verification submission: front-end verification one], we have briefly listed the specific method codes for jQuery to implement form verification submission. So this section will introduce to you in detail the specific method of jQuery to implement form verification submission.
The main code examples are as follows:
<!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>jQuery用户注册表单验证代码</title> <link href="css/jq22.css" rel="stylesheet" type="text/css"/> <script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script> <script language='javascript' src="js/jq22.js"></script> </head> <body> <div class='body_main'> <div class='index_box' style='margin-top:20px;'> <div style="position:fixed;color:red;margin:70px 0 0 450px;font-size:16px;Z-index:100;display:block;" id="hint"></div> <div class='box_title'> <div class='text_content'> <h1>jQuery用户注册表单验证代码</h1> </div> </div> <div class='box_main'> <div id="register" class="register"> <form id="form" action="check1.php" method="post" onSubmit="return check();"> <div id="form_submit" class="form_submit"> <div class="fieldset"> <div class="field-group"> <label class="required title">手机号码</label> <span class="control-group" id="mobile_input"> <div class="input_add_long_background"> <input class="register_input" type="text" id="mobile" name="mobile" maxLength="11" value="" onblur="__changeUserName('mobile');"> </div> </span> <label class="tips">仅用于发送服务开通与到期提醒以及紧急故障方便联系到您,绝对保密</label> </div> <div class="field-group"> <label class="required title">邮箱</label> <span class="control-group" id="email_input"> <div class="input_add_long_background"> <input class="register_input" type="text" id="email" name="email" maxLength="50" value="" onblur="__changeUserName('email');"> </div> </span> <label class="tips">请输入您常用的邮箱</label> </div> <div class="field-group"> <label class="required title">设置密码</label> <span class="control-group" id="password1_input"> <div class="input_add_long_background"> <input class="register_input" type="password" id="password1" name="password1" maxLength="20" value="" onblur="checkPwd1(this.value);"/> </div> </span> <label class="tips">请使用6~20个英文字母(区分大小写)、符号或数字</label> </div> <div class="field-group"> <label class="required title">确认密码</label> <span class="control-group" id="password2_input"> <div class="input_add_long_background"> <input class="register_input" type="password" id="password2" name="password2" maxLength="20" value="" onblur="checkPwd2(this.value);"/> </div> </span> <label class="tips">请输入确认密码,要和上面的密码一致</label> </div> </div> </div> <div id="div_submit" class="div_submit"> <div class='div_submit_button'> <input id="submit" type="submit" value="注册" class='button_button disabled'> </div> </div> </form> </div> <script type="text/javascript"> function __changeUserName(of) { var username = $('#' + of).val(); if (of == 'email') { if (username.search(/^[\w\.+-]+@[\w\.+-]+$/) == -1) { showTooltips('email_input', '请输入正确的Email地址'); return; } } else { if (username == '' || !isMobilePhone(username)) { showTooltips('mobile_input', '请输入正确的手机号码'); return; } } } function checkPwd1(pwd1) { if (pwd1.search(/^.{6,20}$/) == -1) { showTooltips('password1_input', '密码为空或位数太少'); } else { hideTooltips('password1_input'); } } function checkPwd2(pwd2) { var pas1 = $('#password1').val(); if (pwd2.search(/^.{6,20}$/) == -1) { showTooltips('password2_input', '密码为空或位数太少'); } if (pwd2 != pas1) { showTooltips('password2_input', '两次密码不一致'); } } function check() { hideAllTooltips(); var ckh_result = true; // if ($('#email').val() == '' || !isEmail($('#email').val())) { // showTooltips('email_input', '请输入正确的Email地址'); // ckh_result = false; // } if ($('#password1').val() == '') { showTooltips('password1_input', '密码不能为空'); ckh_result = false; } if ($('#password2').val() == '') { showTooltips('password2_input', '确认密码不能为空'); ckh_result = false; } if ($('#password2').val() != $('#password1').val()) { showTooltips('password2_input', '两次密码不一致'); ckh_result = false; } if ($('#mobile').val() == '' || !isMobilePhone($('#mobile').val())) { showTooltips('mobile_input', '手机号码不正确'); ckh_result = false; } return ckh_result; } function isMobilePhone(value) { if (value.search(/^(\+\d{2,3})?\d{11}$/) == -1) { return false; } else return true; } function isEmail(value) { if (value.search(/^[\w\.+-]+@[\w\.+-]+$/) == -1) { return false; } else return true; } </script> </div> <div class='box_bottom'></div> </div> </div> </body> </html>
In the above code, we simply wrote a form and submitted the front-end data to check1.php. Submit The method is post, and a click event is given to the form.
When we click submit, a series of information judgments will be made. And when we do not click to register, that is, the click event is not triggered, there will also be a judgment.
Such as the __changeUserName method in the code. In this method, we first use regular expressions to determine whether the email format is correct and whether the mobile phone number is correct. And in the checkPwd1 and checkPwd2 methods, determine whether the password meets the requirements and is the same as the first password. These methods are all verifications that occur when the registration button is not clicked.
Of course, these judgments are not enough, so we need to write a verification that will occur when clicking to register, such as the check method in the above code, in which we judge each field (required in the background) fields need to be verified).
In addition to the above verification method of click registration, we can also bind an event to the click registration button to determine verification. I won’t go into details here.
The js and css codes involved are all in the previous article [jQuery form verification submission: front-end verification one]. Friends who need it can refer to it.
This article is about jQuery's method of implementing form verification and submission. I hope it will be helpful to friends in need!
If you want to learn more about front-end related knowledge, you can follow the PHP Chinese website jQuery video tutorial, JavaScript video tutorial, Bootstrap tutorialWait for related video tutorials, everyone is welcome to refer to and learn!
The above is the detailed content of jQuery form verification submission: front-end verification 2 (image, text + video). For more information, please follow other related articles on the PHP Chinese website!