JavaScript (3) - timer, form validation

Keywords: ascii Attribute

When outputting strings and variables, strings must be quoted, variables written directly, and connected with connectors.
1.Date object
new.Date() gets the current system date
new.Date.getFullYear() returns the year in four digits
new.Date.getMonth() returns the month < 0 ~ 11 >
new.Date.getDate() returns a day of a month < 1 ~ 31 >
new.Date.getHours() return hours < 0 ~ 23 >
new.Date.getMinutes() returns minutes < 0 ~ 59 >
new.Date.getSeconds() returns seconds < 0 ~ 59 >
new.Date.getDay () returns a day of the week <0~6>
eg:

<script>
    var m=new Date;
    console.log(m);
    document.write("Now it's:"+ m.getFullYear()+"year"+ (m.getMonth()+1)+"month"+ m.getDate()+"day"+ m.getHours()+":"+ m.getMinutes()+":"+ m.getSeconds()+" week"+ m.getDay()+"<br/>");
    document.write(m.toLocaleString()+"<br/>");
    document.write(m.toLocaleDateString()+"<br/>");
    document.write(m.toLocaleTimeString()+"<br/>");
</script>

2. Timer
Start: window.setInterval("function name", 1000); it is timed in 1000ms (unit: ms).
Stop: clearinterval (timer variable name), clearTimeout(timer);
setInterval() - the number of milliseconds specified in the interval keeps executing the specified code and writing it outside the function;
setTimeout() - execute the specified code after pausing the specified number of milliseconds, and write it in the function
Clear and add timers should have the same name, or the cycle will get faster and faster.

3.String object
Properties:

string.length 
object.prototype.name=value  Custom properties

method:

string.charAt(index)            Returns the character at the specified location  //index indicates the location, starting from 0
string.charCodeAt(position)         Returns the character at the specified location unicode Code
string.concat(str1,str2)        Method to connect two or more strings
String.fromCharCode(ASCII code)      Put the designated unicode Code to string
string.indexOf(The string to find,[Starting position])  Returns the first occurrence of a specified character in a string, or if it is not found-1;
string.lastIndexOf()        Find characters after
string.slice(star,end)  Intercepts part of a string and returns a new string;star Indicates the starting position of the intercept,endIndicates the end position of the intercept,Location from0start,Head without tail;
string.split(x,[y])     Split string into string array;x Represents the divided reference object, y Is optional, y Indicates that the setting is divided into array numbers;
string.substr(a,l)      Extract string, a Indicates the starting index location, l Represents the length of the string to extract;
string.substring(s,e)   Extract string s Indicates the starting index position, e Indicates the ending index location, including s,Not included e position,Head without tail
string.replace(String to replace,New string)    Finds the string matching the specified string and returns the string after replacing the matched string with a new string (only the first one is replaced)
string.valueOf()          Return initial value
var array1=[1,2,3,4,5];
array1.splice(2,2,6)     Returned is the deleted element

4.Array object
definition:

 var m=[2,23,6,11,97,65];   // Simple way
 var m=new Array(); 
 m[0]=2;
 m[1]=23;

method:

concat()     Connect two or more arrays;
pop()        Delete last element of array;
shift()      Delete first element of array;
push()      Add one or more elements to the end of the array;
unshift()  Add one or more elements to the beginning of an array;
splice() Method to insert, delete, or replace elements of an array;
splice(index,how,[news])
        // index Indicates where to add or remove
       // how is0Indicates adding, if the number is greater than0Indicates the number of deletions. If this parameter is not specified, the number of deletions from the index All elements from the beginning to the end of the original array.
     // news is optional, indicating the added element;
reverse() Method to reverse the order of elements in an array;
join() Method to convert all elements of an array into a string.

5. Form validation

<form  novalidate></form>

No validate in form disables browser default validation
Required is a required item. It is the default attribute of the browser. It will no longer be valid after adding novalidate.

<form action="" method="get" onsubmit="return checkform()" novalidate>
    <p><label for="username">
    //user name:</label><input type="text" class="username" id="username"/> 
    <span class="usertext"></span>
    </p>
    <p><label for="pwd"> 
    //password:<input type="password" class="pwd" id="pwd"/></label> 
    <span class="pwdtext"></span> 
    </p>
    <p><label for="email"> 
    //Email:<input type="email" class="email" id="email"/></label> 
    <span class="emailtext"></span> 
    </p>
    <p><input type="submit"/> <input type="reset"/></p>
</form>
<script>
    function checkform() {
        var user = document.getElementsByClassName("username")[0];  //User name verification
        var pwd = document.getElementsByClassName("pwd")[0];
        var email = document.getElementsByClassName("email")[0];
        if (user.value.length == 0) {
           document.getElementsByClassName("usertext")[0].innerHTML = "User name cannot be empty!";
            return false;
        }
        else {
            for (var i = 0; i < user.value.length; i++) {
                var one = user.value.trim().toLowerCase().charAt(i);
                // one extracts each character of input, trim() removes the spaces on both sides.
                if ((!(one >= 0 && one <= 9)) && (!(one >= "a" && one <= "z")) && (one != "_")) {
                    document.getElementsByClassName("usertext")[0].innerText = "Incorrect format of user name!";
                    return false;
                }
            }
        }   // Password verification
        if (pwd.value.length == 0) {
            document.getElementsByClassName("pwdtext")[0].innerHTML = "Password cannot be empty!";
            return false;
        }
        else if (!(pwd.value.length >= 6 && pwd.value.length <= 12)) {
            document.getElementsByClassName("pwdtext")[0].innerText = "Password must be 6-12 Bit!";
            return false;
        }  // Mailbox verification
        if(email.value.indexOf("@")==-1||email.value.indexOf(".")==-1){

//For this negative statement, use "or" | ", both of which must be established to pass the verification; use" and & ", only one of which can pass the verification.
            document.getElementsByClassName("emailtext")[0].innerText="Email format is incorrect!";
            return false;
        }
    }
</script>

Posted by suresh64633 on Fri, 01 May 2020 10:36:38 -0700