JQuery verifies whether the jsp page property is null (instance code)

Source: Internet
Author: User

Note:Ensure that the verification is performed before the form is submitted. If there are null items in the verification result, the form cannot be submitted and a verification error message is prompted, when the verification page property is not empty, the form is submitted normally. This part is a JS part and has nothing to do with the background business logic layer and control layer.

You can restrict the submission by using the form label or by adding the onclick event restriction on the submit button:

<Form action = ".." method = "post" onsubmit = "return check ();">
<Input type = "submit" value = "submit" onclick = "return check ()">

Case:
Copy codeThe Code is as follows:
<Form action = "./login. action" method = "post" onsubmit = "return check ();">
<Table>
<Tr>
<Td width = "451"> </td>
& Lt; td width = "378" & gt;

<Table>
<Tr>
<Td width = "60"> </td>
<Td>
<Font color = "#000000"> User name: </font>
</Td>
<Td align = "left"> <input name = "userInfo. userName" id = "userName" type = "text">
<Span id = "checkUserName"> </span>
</Td>
<Tr> <td> </td>
<Td align = "right" nowrap>
<Font color = "#000000"> password: </font>
</Td>
<Td align = "left">
<Input name = "userInfo. password" id = "password" type = "password">
<Span id = "checkPassword"> </span>
</Td>
</Tr>
<Tr>
<Td colspan = "4" align = "center" nowrap>
<Input type = "submit" id = "btnLog" value = "login">
<Input type = "reset" value = "reset">
</Td>
</Tr>
</Table>
</Form>

<! -- Introduce the js file jquery -->
Copy codeThe Code is as follows:
<Script type = "text/javascript" src = "js/common/jquery-1.5.1.min.js"> </script>

<Script type = "text/javascript">
Function check (){
Var userName = $ ("# userName"). val ();
Var password = $ ("# password"). val ();
Alert (userName );
Alert (password );
If (userName = ""){
$ ("# CheckUserName" ).html ("<font color = 'red'> the user name cannot be blank! </Font> ");
Return false;
}
If (password = ""){
$ ("# CheckPassword" ).html ("<font color = 'red'> the password cannot be blank! </Font> ");
Return false;
}
Return true;
}
</Script>

Related Article

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.