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.