Exercise VI - Form Validation
Let’s create an example where we validate user input in a form using TypeScript. This example will demonstrate how to handle input
and submit
events with proper type annotations.
Instructions:
- Create a TypeScript File:
- Create a new file named
formValidation.ts
in yoursrc
directory.
- HTML Setup:
- Create an HTML file in your
dist
folder to house the form - Ensure your HTML file includes a form with an input field for the username and a
feedback
element. - Connect the expected
formValidation.js
to your page using a script tag
html
<!-- Example HTML --><form id="myForm"><label for="username">Username:</label><input type="text" id="username" name="username" /><p id="feedback"></p><button type="submit">Submit</button></form><!-- Add the expected compiled JavaScript file --><script src="./dist/formValidation.js"></script>
- Fill in the ?? to complete the following Code in
formValidation.ts
:
ts
// formValidation.ts// Step 1: Select the form and input elements by their *IDs*const form = ??;const usernameInput = ??;const feedback = ??;// Step 2: Add an event listener to validate the username input in real-timeusernameInput.addEventListener("input", (event: ?) => {const target = ??;if (target ??) {// If the username is shorter than 3 characters, the feedback text will display in red, instructing the user to lengthen the input.??} else {// If the username meets the criteria, the feedback text will display in green, indicating the input is valid.??}});// Step 3: Handle form submission and prevent it if username validation failsform.addEventListener(??,(?)) => {?? // Prevent form from submittingif (usernameInput.?? ) {??}});
Expected Output
When using the form:
- If the username is shorter than 3 characters, the feedback text will display in red, instructing the user to lengthen the input.
- If the username meets the criteria, the feedback text will display in green, indicating the input is valid.
- Submitting the form with an invalid username will trigger an alert message, preventing submission.
- **Ensure the Compiled JavaScript file (
formValidation.js
) is referenced correctly in your HTML file as shown in theHTML Setup
step above.
Check Solution
After completing the code and inserting the compiled script into your HTML file, open your HTML file in a browser to see the result. As you type in the input field, you should see real-time feedback.
The Solution:
ts
// formValidation.ts// Step 1: Select the form and input elements by their IDsconstform =document .getElementById ("myForm") asHTMLFormElement ;constusernameInput =document .getElementById ("username") asHTMLInputElement ;constfeedback =document .getElementById ("feedback") asHTMLElement ;// Step 2: Add an event listener to validate the username input in real-timeusernameInput .addEventListener ("input", (event :Event ) => {consttarget =event .target asHTMLInputElement ;if (target .value .length < 3) {feedback .textContent = "Username must be at least 3 characters long.";feedback .style .color = "red";} else {feedback .textContent = "Username looks good!";feedback .style .color = "green";}});// Step 3: Handle form submission and prevent it if validation failsform .addEventListener ("submit", (event :SubmitEvent ) => {event .preventDefault (); // Prevent form from submittingif (usernameInput .value .length < 3) {alert ("Please enter a valid username.");} else {alert ("Form submitted successfully!");}});