JS Development Validation Form Tutorial - Validation Form (4)
In the previous section we talked about controlling the length of user names
Let’s take a look at how to control the password format and email address through js regular expressions
Even if you don’t know how to write regular expressions Expressions don’t matter. Nowadays, you can find many ready-made regular expressions for email addresses on the Internet.
Let’s take a look at the password. For example, I only need a password that is a combination of numbers and letters and should be between 6 and 10 characters.
##var rel = /^[a-zA-Z0-9]{6,10}$/ ; Let’s take a look at the regular expression of the mailbox var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+) *\.[A-Za-z0-9]+$/; We have completed the regular expression, so how to verify our form? We want to get the value of the text box, and then match it with our regular expression. If it matches, it is in compliance with the specification. If it does not match, a prompt message will be given. Here we will use test() method The test() method is used to retrieve whether a string matches a certain pattern Let’s verify the password, which is a combination of numbers and letters var rel = /^[A-Za-z0-9]{6,10}$/;var val1 = document.getElementById('pwd').value;
#DOCUMENT.GetelementByid ("SP1"). Innerhtml = "Please enter the password!";
} else if (! REL.TEST (VAL1)) {
DOCUMENT.GetelementByid ("SP1"). InnerHtml = " The password does not comply with the specification! ";
}
With the above code, we have completed the verification of the password
Let’s look at the verification email address, which is actually similar to the method of verifying the password
var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za- z0-9]+)*\.[A-Za-z0-9]+$/;
var val2 = document.getElementById('email').value;if(val2 == "") {
document.getElementById("sp2").innerHTML = "Please enter your email address!";
}else if(!reg.test(val2)){
document.getElementById("sp2"). innerHTML = "The email format is incorrect!";
}
With the above code, we have completed the function of verifying the email address
The complete code is as follows:

