Sometimes when we register an account and log in to the system, all verifications can be submitted before we can submit it. This requires Jquery to implement form verification. Today, I will share with you a piece of code to implement form verification based on Jquery.
<html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>Reg</title> <style> .state1{ color:#aaa; } .state2{ color:#000; } .state3{ color:red; } .state4{ color:green; } </style> <script src=""></script> <script> $(function(){ var ok1=false; var ok2=false; var ok3=false; var ok4=false; // Verify username $('input[name="username"]').focus(function(){ $(this).next().text('The username should be between 3-20 digits').removeClass('state1').addClass('state2'); }).blur(function(){ if($(this).val().length >= 3 && $(this).val().length <=12 && $(this).val()!=''){ $(this).next().text('Input is successful').removeClass('state1').addClass('state4'); ok1=true; }else{ $(this).next().text('The username should be between 3-20 digits').removeClass('state1').addClass('state3'); } }); //Verify password $('input[name="password"]').focus(function(){ $(this).next().text('The password should be between 6-20 digits').removeClass('state1').addClass('state2'); }).blur(function(){ if($(this).val().length >= 6 && $(this).val().length <=20 && $(this).val()!=''){ $(this).next().text('Input is successful').removeClass('state1').addClass('state4'); ok2=true; }else{ $(this).next().text('The password should be between 6-20 digits').removeClass('state1').addClass('state3'); } }); //Verify password $('input[name="repass"]').focus(function(){ $(this).next().text('The confirmation password entered must be the same as the password above, and the rules must be the same').removeClass('state1').addClass('state2'); }).blur(function(){ if($(this).val().length >= 6 && $(this).val().length <=20 && $(this).val()!='' && $(this).val() == $('input[name="password"]').val()){ $(this).next().text('Input is successful').removeClass('state1').addClass('state4'); ok3=true; }else{ $(this).next().text('The confirmation password entered must be the same as the password above, and the rules must be the same').removeClass('state1').addClass('state3'); } }); //Verify the email address $('input[name="email"]').focus(function(){ $(this).next().text('Please enter the correct EMAIL format').removeClass('state1').addClass('state2'); }).blur(function(){ if($(this).val().search(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/)==-1){ $(this).next().text('Please enter the correct EMAIL format').removeClass('state1').addClass('state3'); }else{ $(this).next().text('Input is successful').removeClass('state1').addClass('state4'); ok4=true; } }); //Submit button, all verifications can be passed before submitting $('.submit').click(function(){ if(ok1 && ok2 && ok3 && ok4){ $('form').submit(); }else{ return false; } }); }); </script> </head> <body> <form action='' method='post' > use household name:<input type="text" name="username"> <span class='state1'>请输入usehouseholdname</span><br/><br/> password:<input type="password" name="password"> <span class='state1'>请输入password</span><br/><br/> 确认password:<input type="password" name="repass"> <span class='state1'>请输入确认password</span><br/><br/> Mail:<input type="text" name="email"> <span class='state1'>请输入Mail</span><br/><br/> <a href="javascript:;"><img class='submit' type='image' src='./images/' /></a> </form> </body> </html>
The above is all about this article, I hope you like it.