Detailed method for client to verify username and password

  • 2021-06-29 06:17:50
  • OfStack

1. Overview

In the user registration page of a dynamic website, it is often necessary to judge the number of digits and member composition of the user name and password that the user enters, so as to standardize the user's registration information.For example, in this example, the user name is required to be composed of 3-10 bits of letters, numbers and underscores, and the password is composed of 6-20 bits of letters, numbers, underscores and dots "." and the first character is a letter. At this time, the user's input needs to be judged. Therefore, the author has written two functions to verify whether the user name and password entered by the user are legitimate.

2. Technical Essentials

A regular expression that verifies that a user name consists of 3-10 digits of letters, numbers, and underscores is as follows:

/^(\w){3,10}$/

Verify that your password consists of 6-20 digits of letters, numbers, underscores, and dots'."Regular expressions are as follows:

/^[A-Za-z]{1}([A-Za-z0-9]|[._]){5,19}$/

3. Specific implementation code

(1) Write a function checkeusername () using JavaScript to verify the validity of the user name. The function only has one parameter, username, to get the input user name, and the return value is true or false.The code is as follows:


<script language="javascript">
function checkeusername(username){
var str=username;
// stay JavaScript In, regular expressions can only be used "/" You cannot use double quotation marks at the beginning and end 
var Expression=/^(\w){3,10}$/; 
var objExp=new RegExp(Expression); // RegExp 
if(objExp.test(str)==true){ // Verify by regular expression 
return true;
}else{
return false;
}
}
</script> 

(2) Write a function checkePWD() using JavaScript to verify the validity of the password. The function only has one parameter PWD, which is used to get the input password and the return value is true or false.The code is as follows:


<script language="javascript">
function checkePWD(PWD){
var str=PWD;
// stay JavaScript In, regular expressions can only be used "/" You cannot use double quotation marks at the beginning and end 
var Expression=/^[A-Za-z]{1}([A-Za-z0-9]|[._]){5,19}$/; 
var objExp=new RegExp(Expression); // RegExp 
if(objExp.test(str)==true){ // Verify by regular expression 
return true;
}else{
return false;
}
}
</script> 

(3) Call the checkeusername() function and the checkePWD() function to determine whether the user name and password entered by the user are legal, and if they are not, prompt information will be given.The key codes are as follows:


<script language="javascript">
function check(myform){
if(myform.username.value==""){
alert(" enter one user name !");myform.username.focus();return;
}
if(!checkeusername(myform.username.value)){
alert(" The username you entered is not valid !");myform.username.focus();return;
}
if(myform.PWD.value==""){
alert(" Please input a password !");myform.PWD.focus();return;
}
if(!checkePWD(myform.PWD.value)){
alert(" The password you entered is illegal !");myform.PWD.focus();return;
}
if(myform.PWD1.value==""){
alert(" Please confirm your password !");myform.PWD1.focus();return;
}
if(myform.PWD1.value!=myform.PWD.value){
alert(" The password you entered twice is not 1 To, please re-enter !");myform.PWD.focus();return;
}
myform.submit();
}
</script>

Related articles: