I haven't done any projects recently, so I have some spare time. The editor has sorted out the commonly used js form verification code in daily life and shared it on the Wulin Network platform for everyone to learn. For friends who need it, please refer to it!
Registration Verification:
<script language="JavaScript" src="js/jquery-1.9.1.min.js" type="text/javascript"></script>//Verify form function vailForm(){var form = jQuery("#editForm");if(!vailNickName())return;if(!vailPhone())return;if(!vailPwd())return;if(!vailConfirmPwd())return;if(!vailEmail())return;if(!vailCode())return;if(!vailAgree())return;form.submit();}//Verify nickname function vailNickName(){var nickName = jQuery("#nickName").val();var flag = false;var message = "";var patrn=/^/d+$/;var length = getNickNameLength();if(nickName == ''){message = "The nickname cannot be empty!";}else if(length<4||length>16){message = "Nickname 4-16 characters!";} else if(checkNickNameIsExist()){message = "The nickname already exists, please re-enter!";}else{flag = true;}if(!flag){jQuery("#nickNameDiv").removeClass().addClass("ui-form-item has-error");jQuery("#nickNameP").html("");jQuery("#nickNameP").html("<i class=/"icon-error ui-margin-right10/"><//i>"+message);//jQuery("#nickName").focus();}else{jQuery("#nickNameDiv").removeClass().addClass("ui-form-item has-success");jQuery("#nickNameP").html("");jQuery("#nickNameP").html("<i class=/"icon-success ui-margin-right10/"><//i>This nickname is available");} return flag;}//calculate the nickname length function getNickNameLength(){var nickName = jQuery("#nickName").val();var len = 0;for (var i = 0; i < nickName.length; i++) {var a = nickName.charAt(i); //Function format: stringObj.match(rgExp) stringObj is a string, rgExp is a regular expression required //Return value: If it can match, return the result array, and if it cannot match, return nullif (a.match(/[^/x00-/xff]/ig) != null){len += 2;}else{len += 1;}}return len;}//Verify whether the nickname exists function checkNickNameIsExist(){var nickName = jQuery("#nickName").val();var flag = false;jQuery.ajax({ url: "checkNickName?t=" + (new Date()).getTime(),data:{nickName:nickName},dataType:"json",type:"GET",async:false,success:function(data) {var status = data.status;if(status == "1"){flag = true;}}});return flag;}//Verify mobile phone number function vailPhone(){var phone = jQuery("#phone").val();var flag = false;var message = "";//var myreg = /^(((13[0-9]{1})|159|153)+/d{8})$/;var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0-9]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-3]{1})|(18[5-9]{1})|(18[5-9]{1}))+/d{8})$/;if(phone == ''){message = "The mobile phone number cannot be empty!";}else if(phone.length !=11){message = "Please enter a valid mobile phone number!";}else if(checkPhoneIsExist()){message = "Please enter a valid mobile phone number!";}else if(checkPhoneIsExist()){message = "The mobile phone number has been bound!";}else{flag = true;}if(!flag){jQuery("#phoneDiv").removeClass().addClass("ui-form-item has-error");jQuery("#phoneP").html("");jQuery("#phoneP").html("<i class=/"icon-error ui-margin-right10/"><//i>"+message);//jQuery("#phone").focus();}else{jQuery("#phoneDiv").removeClass().addClass("ui-form-item has-success");jQuery("#phoneP").html("");jQuery("#phoneP").html("<i class=/"icon-success ui-margin-right10/"><//i>This mobile phone number is available");}return flag;}//Verify whether the mobile phone number has function checkPhoneIsExist(){var phone = jQuery("#phone").val();var flag = true;jQuery.ajax({ url: "checkPhone?t=" + (new Date()).getTime(),data:{phone:phone},dataType:"json",type:"GET",async:false,success:function(data) {var status = data.status;if(status == "0"){flag = false;}}});return flag;}//Verify password function vailPwd(){var password = jQuery("#password").val();var flag = false;var message = "";var patrn=/^/d+$/;if(password ==''){message = "Password cannot be empty!";}else if(password.length<6 || password.length>16){message = "Password 6-16 bits!";}else if(patrn.test(password)){message = "Password cannot be all numbers!";}else{flag = true;}if(!flag){jQuery("#passwordDiv").removeClass().addClass("ui-form-item has-error");jQuery("#passwordP").html("");jQuery("#passwordP").html("<i class=/"icon-error ui-margin-right10/"><//i>"+message);//jQuery("#password").focus();}else{jQuery("#passwordDiv").removeClass().addClass("ui-form-item has-success");jQuery("#passwordP").html("");jQuery("#passwordP").html("<i class=/"icon-success ui-margin-right10/"><//i>This password is available");}return flag;}//Verify confirm password function vailConfirmPwd(){var confirmPassword = jQuery("#confirm_password").val();var patrn=/^/d+$/;var password = jQuery("#password").val();var flag = false;var message = "";if(confirmPassword == ''){message = "Please enter the confirmation password!";}else if(confirmPassword != password){message = "The input of the secondary password is inconsistent, please re-enter!";}else if(patrn.test(password)){message = "The password cannot be all numbers!";}else {flag = true;}if(!flag){jQuery("#confirmPasswordDiv").removeClass().addClass("ui-form-item has-error");jQuery("#confirmPasswordP").html("");jQuery("#confirmPasswordP").html("<i class=/"icon-error ui-margin-right10/"><//i>"+message);//jQuery("#confirm_password").focus();}else{jQuery("#confirmPasswordDiv").removeClass().addClass("ui-form-item has-success");jQuery("#confirmPasswordP").html("");jQuery("#confirmPasswordP").html("<i class=/"icon-success ui-margin-right10/"><//i>The password is correct");}return flag;}//Verify mailbox function vailEmail(){var email = jQuery("#email").val();var flag = false;var message = "";var myreg = /^([/.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(/.[a-zA-Z0-9_-])+/; if(email ==''){message = "The email cannot be empty!";}else if(!myreg.test(email)){message = "Please enter a valid email address!";}else if(checkEmailIsExist()){message = "This email address has been registered!";}else{flag = true;}if(!flag){jQuery("#emailDiv").removeClass().addClass("ui-form-item has-error");jQuery("#emailP").html("");jQuery("#emailP").html("<i class=/"icon-error ui-margin-right10/"><//i>"+message);//jQuery("#email").focus();}else{jQuery("#emailDiv").removeClass().addClass("ui-form-item has-success");jQuery("#emailP").html("");jQuery("#emailP").html("<i class=/"icon-success ui-margin-right10/"><//i>This mailbox is available");}return flag;}//Verify whether the mailbox has function checkEmailIsExist(){var email = jQuery("#email").val();var flag = false;jQuery.ajax({ url: "checkEmail?t=" + (new Date()).getTime(),data:{email:email},dataType:"json",type:"GET",async:false,success:function(data) {var status = data.status;if(status == "1"){flag = true;}}});return flag;}//Verification verification code function vailCode(){var randCode = jQuery("#randCode").val();var flag = false;var message = "";if(randCode == ''){message = "Please enter the verification code!";}else if(!checkCode()){message = "The verification code is incorrect!";}else{flag = true;}if(!flag){jQuery("#randCodeDiv").removeClass().addClass("ui-form-item has-error");jQuery("#randCodeP").html("");jQuery("#randCodeP").html("<i class=/"icon-error ui-margin-right10/"><//i>"+message);//jQuery("#randCode").focus();}else{jQuery("#randCodeDiv").removeClass().addClass("ui-form-item has-success");jQuery("#randCodeP").html("");jQuery("#randCodeP").html("<i class=/"icon-success ui-margin-right10/"><//i>");}return flag;}//Check whether the random verification code is correct function checkCode(){var randCode = jQuery("#randCode").val();var flag = false;jQuery.ajax({ url: "checkRandCode?t=" + (new Date()).getTime(),data:{randCode:randCode},dataType:"json",type:"GET",async:false,success:function(data) {var status = data.status;if(status == "1"){flag = true;}}});return flag;}//Judge whether function vailAgree(){if(jQuery("#agree").is(":checked")){return true;}else{alert("Please confirm whether to read and agree to the XX protocol");}return false;}function delHtmlTag(str){ var str=str.replace(/<//?[^>]*>/gim,"");//Remove all html tags var result=str.replace(/(^/s+)|(/s+$)/g,"");//Remove the spaces before and after return result.replace(//s/g,"");//Remove the middle space of the article}<!DOCTYPE html><html><body><h1>My first paragraph of JavaScript</h1><p>Please enter the number. If the input value is not a number, the browser will pop up a prompt box. </p><input id="demo" type="text"><script>function myFunction(){var x=document.getElementById("demo").value;if(x==""){ alert("Input cannot be empty"); return;}if(isNaN(x)){ alert("Please enter number"); return;}if(x.length!=6){ alert("Please enter 6 digits"); return;}}</script><button type="button" onclick="myFunction()">Click here</button></body></html> //Verify that the password is underlined by the numeric letters CheckPwd(pwd) {var validStr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_~/!@#$%^&*();-+.=,";for (i = 0; i < pwd.length; i++) {if (validStr.indexOf(pwd.charAt(i)) == -1) {return false;}}return true;}//Verify the mailbox format function checkemail(email) {var filter = /^([a-zA-Z0-9_/./-])+/@(([a-zA-Z0-9/-])+/.)+([a-zA-Z0-9]{2,4})+$/;if (!filter.test(email)) {return false;}return true;}function isEmail(val) {return /^((((([az]|/d|[!#/$%&'/*/+/-/=/?/^_/`{/|}~]|[/u00A0-/uD7FF/uF900-/uFDCF/uFDF0-/uFFEF])+(/.([az]|/d|[!#/$%&'/*/+/-/=/?/^_`{/|}~]|[/u00A0-/uD7FF/uF900-/uFDCF/uFDF0-/uFFEF])+)*)|((/x22)((((((/x20|/x09)*(/x0d/x0a))?(/x20|/x09)+)?(([/x01 -/x08/x0b/x0c/x0e-/x1f/x7f]|/x21|[/x23-/x5b]|[/x5d-/x7e]|[/u00A0-/uD7FF/uF900-/uFDCF/uFDF0-/uFFEF])|(//([/x01-/x09/x0b/x0c/x0d-/x7f]|[/u00A0-/uD7FF/uF900-/uFDCF/uFDF0-/uFFEF])))*((((/x20|/x09)*(/x0d/x0a))?(/x20|/x09)+)?(/x22)))@((((([ az]|/d|[/u00A0-/uD7FF/uF900-/uFDCF/uFDF0-/uFFEF])|(([az]|/d|[/u00A0-/uD7FF/uF900-/uFDCF/uFDF0-/uFFEF])([az]|/d|-|/.|_|~|[/u00A0-/uD7FF/uF900-/uFDCF/uFDF0-/uFFEF])*([az]|/d|[/u00A0-/uD7FF/uF900-/uFDCF/uFDF0-/uFFEF])))/.)+(([az]|[/u000A0-/uD7FF/uF900-/uFDCF/uFDF0-/uFFEF])))/.)+(([az]|[/u00 A0-/uD7FF/uF900-/uFDCF/uFDF0-/uFFEF])|(([az]|[/u00A0-/uD7FF/uF900-/uFDCF/uFDF0-/uFFEF])([az]|/d|-|/.|_|~|[/u00A0-/uD7FF/uF900-/uFDCF/uFDF0-/uFFEF])*([az]|[/u00A0-/uD7FF/uF900-/uFDCF/uFDF0-/uFFEF])))/.?$/.test(val);}////Mobile phone number verification function checktelephone(cellPhone) {var RegCellPhone = /^([0-9]{11})?$/;falg = cellPhone.search(RegCellPhone);if (falg == -1) {return false;} else {return true;}}//Get URL parameter value function getParameter(param) {var query = window.location.search;var iLen = param.length;var iStart = query.indexOf(param);if (iStart == -1)return "";iStart += iLen + 1;var iEnd = query.indexOf("&", iStart);if (iEnd == -1)return query.substring(iStart);return query.substring(iStart, iEnd);}The above code is the js form verification introduced by the editor to you. The code is simple and easy to understand and very practical. I hope it will be helpful to everyone. At the same time, I would like to thank you very much for your support for the Wulin Network website!