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's name and password entered by the user, so as to standardize the user's registration information. For example, this example requires that the user name consists of letters, numbers and underscores of 3-10 digits, and the password is composed of letters, numbers, underscores and dots "." of 6-20 digits, and the first character is a letter. At this time, it is necessary to judge the user's input. Therefore, the author wrote two functions to verify whether the user name and password entered by the user are legal.
2. Technical points
The regular expression that verifys whether the username consists of 3-10-digit letters, numbers and underscores is as follows:
/^(/w){3,10}$/
The regular expression that verifys that the password consists of 6-20-digit letters, numbers, underscores and dots "." is as follows:
/^[A-Za-z]{1}([A-Za-z0-9]|[._]){5,19}$/
3. Specific implementation code
(1) Use JavaScript to write a function checkeusername () to verify whether the username is legal. The function has only one parameter username, which is used to obtain the input username, and the return value is true or false. The code is as follows:
<script language="javascript">function checkeusername(username){var str=username;//In JavaScript, regular expressions can only start and end with "/", and cannot use double quotes var Expression=/^(/w){3,10}$/; var objExp=new RegExp(Expression); //Create regular expression object if(objExp.test(str)==true){ //Verify return true through regular expressions;}else{return false;}}</script>(2) Use JavaScript to write a function checkPWD() to verify whether the password is legal. The function has only one parameter PWD, which is used to obtain 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;//In JavaScript, regular expressions can only start and end with "/", and cannot use double quotes var Expression=/^[A-Za-z]{1}([A-Za-z0-9]|[._]){5,19}$/; var objExp=new RegExp(Expression); //Create regular expression object if(objExp.test(str)==true){ //Verify return true through regular expressions;}else{return false;}}</script>(3) Call the checkusername() function and checkPWD() function to determine whether the user name and password entered by the user are legal. If it is not legal, a prompt message will be given. The key code is as follows:
<script language="javascript">function check(myform){if(myform.username.value==""){alert("Please enter the username!");myform.username.focus();return;}if(!checkeusername(myform.username.value)){alert("The username you entered is illegal!");myform.username.focus();return;}if(myform.PWD.value==""){alert("Please enter the password!");myform.PWD.focus();return;}if(!che ckePWD(myform.PWD.value)){alert("The password you entered is illegal!");myform.PWD.focus();return;}if(myform.PWD1.value==""){alert("Please confirm the password!");myform.PWD1.focus();return;}if(myform.PWD1.value!=myform.PWD.value){alert("The password you entered twice is inconsistent, please re-enter!");myform.PWD.focus();return;}myform.submit();}</script>The above is the relevant knowledge about client verification username and password introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!