Part of JavaScript
- ECMAScript: It's the core of JavaScript (syntax, variables, data types, statements, functions ...)
- DOM: (Document Object model) documents objects
- BOM: (Brower object model) browser objects
The syntax of JavaScript
- Case sensitive;
- Weak type;
- Comments are the same as Java;
- define variables using Var;
Data types for JavaScript
- Raw data type: String, number, Boolean, null, undefined
- Application data type: Array, Boolean, Date, Math, number, String, reqexp
The JavaScript operator
- = =: It will be converted automatically when making comparisons.
- = = =: It does not convert automatically when making comparisons.
- Other operators are basically consistent with Java
JavaScript gets element content
Get element: document.getElementById ("id name");
Gets the value inside the element: document.getElementById ("ID name"). Value;
The output of JavaScript
- Warning Box: Alert ("string");
- Writes to the location specified by the page: InnerHTML ("Write content");
- Write content to page: document.write ("Write content");
Form submission events in javascript: onsubmit
Example 1: A simple registration form check
HTML code <form action= "#" method= "Get" name= "RegForm" onsubmit= "return Checkform ()" > <table border= "1px" width= "750px" height= "400px" align= "center" cellpadding= "0px" cellspacing= "0px" bgcolor= "white "> <tr height= "40px" > <TD colspan= "2" > <font size= "4" > member Registration </font> USER REGISTER </td> </tr> <tr> <td> User name </td> <td> <input type= "text" name= "user" size= "34px" id= "user"/> </td> </tr> <tr> <td> Password </td> <td> <input type= "password" name= "password" size= "34px" id= "password"/> </td> </tr> <tr> <td> Confirm Password </td> <td> <input type= "Password" name= "Repassword" size= "34px" id= "Repassword" ></input> </td> </tr> <tr> <td> Emaile </td> <td> <input type= "text" name= "email" size= "34px" id= "Eamil"/> </td> </tr> <tr> <td> Name </td> <td> <input type= "text" name= "username" size= "34px"/> </td> </tr> <tr> <td> Gender </td> <td> <input type= "Radio" name= "Sex" value= "male"/> Male <input type= "Radio" name= "Sex" value= "female"/> Female </td> </tr> <tr> <td> Date of birth </td> <td> <input type= "text" name= "Birthday" size= "34px"/> </td> </tr> <tr> <td> Verification Code </td> <td> <input type= "text" name= "Yzm"/> </td> </tr> <tr> <TD colspan= "2" > <input type= "Submit" value= "register"/> </td> </tr> </table> </form> |
| JavaScript code <script> Function checkform () { //alert ("AA"); /** Verify User name */ //1. Get user-entered data var uvalue = document.getElementById ("user"). Value; //alert (uvalue); If (uvalue== ") { //2. Error message Alert (" User name cannot be empty! "); return false; } /* Check password */ var pValue = document.getElementById ("password"). Value; If (pvalue== ") { Alert (" Password cannot be empty! "); return false; } /** Check Confirm password */ var rpvalue = document.getElementById ("Repassword"). Value; if (rpvalue!=pvalue) { alert ("Two times password input is inconsistent!"); return false; } /* Check mailbox */ var evalue = document.getElementById ("Eamil"). Value; If (!/^ ([a-za-z0-9_-]) [email protected] ([a-za-z0-9_-]) + (. [ A-za-z0-9_-]) +/.test (evalue) { Alert ("The mailbox format is incorrect!"); return false; } } </script> |
Getting Started with JavaScript (i)