CSC378 ยท 2081 ยท 5 marks
Write a program to validate the form that takes input user name, password and phone number using JQuery. All fields must be non-empty and phone number must contain exactly 10 digits.
Write a program to validate the form that takes input user name, password and phone number using JQuery. All fields must be non-empty and phone number must contain exactly 10 digits. [5]
NET Centric ComputingChapter 7: Client-side Development in ASP.NET CoreForms and Validation
jQuery Form Validation - Username, Password, and Phone Number
Complete Solution
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Validation using jQuery</title>
<!-- jQuery CDN -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 50px;
}
.error {
color: red;
font-size: 13px;
}
.success {
color: green;
font-size: 14px;
}
input {
display: block;
margin: 5px 0 2px 0;
padding: 6px;
width: 250px;
}
label {
font-weight: bold;
margin-top: 10px;
display: block;
}
button {
margin-top: 15px;
padding: 8px 20px;
}
</style>
</head>
<body>
<h2>User Registration Form</h2>
<form id="registrationForm">
<label for="username">Username:</label>
<input type="text" id="username" placeholder="Enter username" />
<span class="error" id="usernameError"></span>
<label for="password">Password:</label>
<input type="password" id="password" placeholder="Enter password" />
<span class="error" id="passwordError"></span>
<label for="phone">Phone Number:</label>
<input type="text" id="phone" placeholder="Enter 10-digit phone number" />
<span class="error" id="phoneError"></span>
<button type="submit">Submit</button>
<p id="successMsg" class="success"></p>
</form>
<script>
$(document).ready(function () {
// Handle form submission
$("#registrationForm").submit(function (event) {
// Prevent default form submission
event.preventDefault();
// Clear previous error messages
$(".error").text("");
$("#successMsg").text("");
// Get field values and trim whitespace
var username = $("#username").val().trim();
var password = $("#password").val().trim();
var phone = $("#phone").val().trim();
var isValid = true;
// Validate Username - must be non-empty
if (username === "") {
$("#usernameError").text("Username is required.");
isValid = false;
}
// Validate Password - must be non-empty
if (password === "") {
$("#passwordError").text("Password is required.");
isValid = false;
}
// Validate Phone - must be non-empty AND exactly 10 digits
if (phone === "") {
$("#phoneError").text("Phone number is required.");
isValid = false;
} else if (!/^\d{10}$/.test(phone)) {
$("#phoneError").text("Phone number must contain exactly 10 digits.");
isValid = false;
}
// If all validations pass
if (isValid) {
$("#successMsg").text("Form submitted successfully!");
}
});
});
</script>
</body>
</html>
Explanation of Key Parts
| Part | Purpose |
|---|---|
$(document).ready() | Ensures the DOM is fully loaded before jQuery runs |
event.preventDefault() | Stops the form from reloading the page on submit |
.val().trim() | Gets the field value and removes leading/trailing spaces |
=== "" | Checks if the field is empty (non-empty validation) |
/^\d{10}$/.test(phone) | Regex to check phone has exactly 10 digits only |
isValid flag | Tracks overall validation status before showing success |
Validation Rules Applied
- Username - must not be empty
- Password - must not be empty
- Phone Number - must not be empty AND must contain exactly 10 digits (no letters or special characters allowed)
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.