How to use jquery for simple form validation:
jquery can be used to form validation, of course, there are more complex forms validation, here is not introduced, the following is a brief introduction of how to use jquery simple form verification, and then we can be modified to perform basic expression verification. Let's look at a code example:
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.51texiao.cn/" /><title>Ant Tribe</title><styletype= "Text/css">Body{font-size:11px}#divLogin{margin:15% 0 0 15%}#divLogin fieldset{width:300px;padding:0px;margin:0px}#divLogin fieldset h3{margin:0px;padding:5px;Background-color:#eee}#divLogin fieldset. Content{padding:20px;Line-height:2.6em}#divLogin fieldset. Content. Btncenter{text-align:Center}. txt{Border:#666 1px solid;padding:2px;width:180px;Margin-right:3px}. BTN{Border:#666 1px solid;padding:2px;width:60px;Filter:progid:DXImageTransform.Microsoft.Gradient (gradienttype=0, startcolorstr= #ffffff, endcolorstr= #ECE9D8)}</style><Scripttype= "Text/javascript"src= "Mytest/jquery/jquery-1.8.3.js"></Script><Scripttype= "Text/javascript">$ (document). Ready (function(){ $("#Button1"). Click (function() { //button click event var$name= $("#txtName"); //User name var$pass= $("#txtPass"); //Password if($name. Val ()== "") {alert ("the user name cannot be empty!"); $name. focus (); return false; } Else{alert ("The password cannot be empty!"); $pass. focus (); return false; } }) })</Script></Head><Body><DivID= "Divlogin"> <fieldset> <H3>User Login</H3> <Divclass= "Content"> <Div>User:<inputID= "Txtname"type= "text"class= "txt" /> </Div> <Div>Password:<inputID= "Txtpass"type= "Password"class= "txt"/> </Div> <Divclass= "Btncenter"> <inputID= "Button1"type= "button"value= "Login"class= "BTN" /> <inputID= "Reset1"type= "Reset"value= "Cancel"class= "BTN" /> </Div> </Div> </fieldset></Div></Body></HTML>
The above code implements the basic form verification function, of course, can continue to complicate, but also can make the page more beautiful, will be introduced slowly.
The original address is: http://www.51texiao.cn/jqueryjiaocheng/2015/0504/669.html
How to use jquery for simple form validation