Jsp verification form before submission

Source: Internet
Author: User

When submitting a form, we often need to verify and verify the content of the form. If it is not empty, it can be submitted. If there is a blank text box, It is not submitted and the mouse focus is obtained to the text box, therefore, we can use the onsubmit method. See the following example:

<Html>

<Head> <title> </title>

<Script>

Function yan (){

If (frm. stuname. value = ""){

Alert ("name cannot be blank! "); // A prompt box is displayed.

Document. frm. stuname. focus (); // get the mouse focus

Return (false); // return a value.

}

Else if (frm. stuPWD. value = ""){

Alert ("the password cannot be blank! "); // A prompt box is displayed.

Document. frm. stuPWD. focus (); // get the mouse focus

Return (false); // return a value.

}

 

}

</Script>

</Head>

<Body>

<Form onsubmit = "return yan ()">

<Table>

<Tr>

<Td> User name: <input type = "text" id = "stuname"> </td>

</Tr>

<Tr>

<Td> password: <input type = "password" id = "stuPWD"> </td>

</Tr>

<Tr>

<Td> <input type = "submit" value = "submit"> </td>

</Tr>

<Table>

</Form>

</Body>

</Html>

 

This way, the method will be triggered when you press submit. Note that onsubmit = "return yan ()" must be added with return. Otherwise, the method will become invalid if no return value exists.

 

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.