Jquery verifies whether the form text box is empty

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.