In this project, you will learn how to implement an account verification feature using a mobile phone number verification code. This feature is commonly used in many platforms nowadays to provide users with an additional layer of security and convenience when logging in.
👀 Preview
🎯 Tasks
In this project, you will learn:
How to implement the function to send the verification code
How to implement the function to verify the input verification code
How to understand the usage of the Notification component in Element Plus
🏆 Achievements
After completing this project, you will be able to:
Handle user input and validation
Generate and manage dynamic verification codes
Use the Notification component to display success and error messages
Implement component switching and state management using Vue.js and Pinia
In the setup() function of the phone component, inject the necessary variables and functions:
app.component("phone", {
template: "#phone",
setup() {
// TODO
let isSure = ref("");
let phoneVal = inject("phoneVal");
let code = inject("code");
let createCode = inject("createCode");
let data = inject("data");
return {};
}
});
Implement the verifyPhone() function to check the validity of the phone number:
function verifyPhone(num) {
if (num.length != 11) return false;
return num[0] == 1 && num[1] == 8;
}
Implement the nextStep() function to handle the click of the next button, all the code for the phone component is as follows:
app.component("phone", {
template: "#phone",
setup() {
// TODO
let isSure = ref("");
let phoneVal = inject("phoneVal");
let code = inject("code");
let createCode = inject("createCode");
let data = inject("data");
function verifyPhone(num) {
if (num.length != 11) return false;
return num[0] == 1 && num[1] == 8;
}
return {
isSure,
phoneVal,
nextStep() {
if (!isSure.value)
return ElNotification({
title: "Sent Failed",
message: "Please read and agree to the agreement below",
type: "error"
});
if (!verifyPhone(phoneVal.value))
return ElNotification({
title: "Sent Failed",
message: "Invalid phone number",
type: "error"
});
code.value = createCode();
ElNotification({
title: "Sent Successfully",
message: "Your verification code is " + code.value,
type: "success"
});
data.showName = "check";
}
};
}
});
In this function, we first check if the user has agreed to the terms and conditions. If not, we show an error notification. Then, we check the validity of the phone number using the verifyPhone() function. If the phone number is invalid, we show an error notification. If both conditions are met, we generate a random 6-digit verification code using the createCode() function, show a success notification, and switch the component to the check component.
Implement the Function To Verify the Input Verification Code
In this step, you will learn how to implement the function to verify the input verification code. Follow the steps below to complete this step:
Open the index.html file in the provided project.
Add binding events to the <template id="check"> section:
In this code, we first focus on the first input field for the verification code. Then, we add an oninput event handler to each input field. When the user enters a value, the cursor will automatically jump to the next input field. When the user deletes a value, the cursor will automatically jump to the previous input field.
We also implement the trackVal() function to verify the input verification code. If the input is 6 digits and matches the generated verification code, we show a success notification and switch the component to the success component. If the input is incorrect, we clear the input fields and show an error notification.
Implement the resentCode() function to resend the verification code, all the code for the check component is as follows:
We use cookies for a number of reasons, such as keeping the website reliable and secure, to improve your experience on our website and to see how you interact with it. By accepting, you agree to our use of such cookies. Privacy Policy