Html section
The code is as follows: |
Copy code |
<Form> <Table> <Tr> <Td align = "center"> <input id = "t1" type = "text" size = "5" msg = "name"/> </td> <Td align = "center"> <input id = "t2" type = "text" size = "5" msg = "gender"/> </td> <Td align = "center"> <input id = "t3" type = "text" size = "5" msg = "age"/> </td> <Td align = "center"> <input id = "t4" type = "text" size = "5" msg = "address"/> </td> <Td align = "center"> <input id = "t5" type = "text" size = "5" msg = "mailbox"/> </td> <Td align = "center"> <input id = "t6" type = "text" size = "5" msg = "province"/> </td> </Tr> <Tr> <Td align = "center"> <input type = "submit" onclick = "return checkNull ()"/> </td> </Tr> </Table> <Form> Js section <Script type = "text/javascript"> Function checkNull () { Var num = 0; Var str = ""; $ ("Input [type $ = 'text']"). each (function (n ){ If ($ (this). val () = "") { Num ++; Str + = "? "+ $ (This). attr (" msg ") +" no www.111cn.net can be blank! Rn "; } }); If (num> 0) { Alert (str ); Return false; } Else { Return true; } } </Script> |
Here we are actually imitating the jQuery Validate plug-in to traverse the from and then judge whether it is empty one by one. For example, the core code is
The code is as follows: |
Copy code |
$ ("Input [type $ = 'text']"). each (function (n ){ If ($ (this). val () = "") { Num ++; Str + = "? "+ $ (This). attr (" msg ") +" cannot be blank! Rn "; } }); |
In this example, you can obtain all the input attributes of the current page form and text boxes of the type = text type before making a judgment, this is a simple example of empty form verification. If you want to be more complex, such as the difference in the input of each form, such as the phone number, I can use the jQuery Validate plug-in for email addresses and so on. The following is a simple example.
The code is as follows: |
Copy code |
<Input id = "confirm_password" name = "confirm_password" type = "password" class = "required" login to = "# password"/> <Input id = "email" name = "email" class = "required email"/> |
Note that class = "required email" indicates that the plug-in must be verified.