CSC329 ยท 2079 ยท 10 marks
Create an HTML file containing form elements textbox for username, password field and a checkbox for hobbies. Now write javascript function for the form validation. You should validate for username to be empty, password should be of length at least 5, and the check box should be checked.
Create an HTML file containing form elements textbox for username, password field and a checkbox for hobbies. Now write javascript function for the form validation. You should validate for username to be empty, password should be of length at least 5, and the check box should be checked.[10]
Web TechnologyChapter 4: Client Side Scripting with JavaScriptEvent Handling and Form Validation
HTML Form Validation - Username, Password, and Checkbox
Complete HTML File with JavaScript Validation
<!DOCTYPE html>
<html>
<head>
<title>Form Validation</title>
</head>
<body>
<h2>Registration Form</h2>
<form name="myForm" onsubmit="return validate()">
<!-- Username Field -->
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<!-- Password Field -->
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<!-- Hobbies Checkbox -->
<label>Hobbies:</label>
<input type="checkbox" id="reading" name="hobbies" value="reading"> Reading
<input type="checkbox" id="gaming" name="hobbies" value="gaming"> Gaming
<input type="checkbox" id="coding" name="hobbies" value="coding"> Coding
<!-- Submit Button -->
<input type="submit" value="Submit">
</form>
<script type="text/javascript">
function validate() {
// ---- 1. Username Validation (should not be empty) ----
var username = document.getElementById("username").value;
if (username === "") {
alert("Username cannot be empty. Please enter your username.");
return false;
}
// ---- 2. Password Validation (length at least 5) ----
var password = document.getElementById("password").value;
if (password === "") {
alert("Password cannot be empty. Please enter your password.");
return false;
}
if (password.length < 5) {
alert("Password must be at least 5 characters long.");
return false;
}
// ---- 3. Checkbox Validation (at least one must be checked) ----
var hobbies = document.getElementsByName("hobbies");
var isChecked = false;
for (var i = 0; i < hobbies.length; i++) {
if (hobbies[i].checked) {
isChecked = true;
break;
}
}
if (!isChecked) {
alert("Please select at least one hobby.");
return false;
}
// All validations passed
alert("Form submitted successfully!");
return true;
}
</script>
</body>
</html>
Explanation of Each Part
1. Form Structure
| Element | Type | Purpose |
|---|---|---|
<input type="text"> | Text box | Accepts username input |
<input type="password"> | Password field | Accepts password (masked) |
<input type="checkbox"> | Checkbox | Allows hobby selection |
onsubmit="return validate()" | Event handler | Calls validation before submit |
Note: The
onsubmitevent handler calls thevalidate()function. If the function returnsfalse, the form is NOT submitted. This is standard client-side validation.
2. Validation Logic Summary
Username Validation:
if (username === "") {
alert("Username cannot be empty.");
return false;
}
- Checks if the username field is left blank.
- Returns
falseto stop form submission.
Password Validation:
if (password.length < 5) {
alert("Password must be at least 5 characters long.");
return false;
}
- First checks if empty, then checks minimum length of 5.
Checkbox Validation:
for (var i = 0; i < hobbies.length; i++) {
if (hobbies[i].checked) {
isChecked = true;
break;
}
}
if (!isChecked) {
alert("Please select at least one hobby.");
return false;
}
- Uses
getElementsByName()to get all checkboxes with name"hobbies". - Loops through each checkbox to check if at least one is selected.
- If none is checked, shows alert and returns
false.
3. Flow Diagram
User clicks Submit
|
v
Username empty? --> YES --> Alert + return false (stop)
|
NO
|
v
Password empty? --> YES --> Alert + return false (stop)
|
NO
|
v
Password length < 5? --> YES --> Alert + return false (stop)
|
NO
|
v
Any checkbox checked? --> NO --> Alert + return false (stop)
|
YES
|
v
Form submitted successfully (return true)
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.