7 Client Side Development In Asp Net Core

NET Centric Computing · Unit 7 · 4 hrs

Client-side Development in ASP.NET Core

Exam-focused notes for Client-side Development in ASP.NET Core (NET Centric Computing, CSC378): what the TU syllabus asks and how it has actually been tested, with 5 solved past questions from this unit.

Client-side Development in ASP.NET Core in short

This unit turns to the browser side of an ASP.NET Core application: the standard client-side web technologies, jQuery as a practical DOM and AJAX library, form validation on the client, and the single page application frameworks that increasingly replace full-page reloads.

Client-side Development in ASP.NET Core: notes

Common client-side web technologies

ASP.NET Core applications are still web applications at the browser, so they typically rely on the standard client-side trio: HTML for content, CSS for layout and styling, and JavaScript for behaviour. Keeping these separate lets a complex web app follow the separation of concerns principle, with each layer changed independently of the others. While HTML and CSS have stayed relatively stable, JavaScript has grown a large ecosystem of frameworks and libraries that developers reach for to build modern web front ends, most notably Angular, jQuery, React, and Vue.

JQuery

jQuery is a fast, small, feature-rich JavaScript library distributed as a single script file. It saves developers from writing large amounts of raw JavaScript for common tasks by wrapping them in simple, chainable functions. Its main capabilities are DOM selection (retrieving elements by tag name, id, CSS class, attribute, position, and so on), DOM manipulation (adding, removing, or modifying elements and their HTML/CSS), special effects (show/hide, fade in/out, sliding, animation), events (equivalents of native DOM events such as click, mouseenter, and keyup that automatically smooth over cross-browser differences), and easy-to-use AJAX functions for loading data from a server without reloading the whole page.

What this unit covers

  • Common client-side web technologies
  • JQuery
  • Forms and Validation
  • Single Page Application (SPA) Frameworks: Angular, React

Forms and Validation

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. [5]

Show solved answer

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

PartPurpose
$(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 flagTracks overall validation status before showing success

Validation Rules Applied

  1. Username - must not be empty
  2. Password - must not be empty
  3. Phone Number - must not be empty AND must contain exactly 10 digits (no letters or special characters allowed)
Open this answer on its own page →

2078 · 5 marks

Why do we need to validate the form? How can you use react for this? [5]

Form validation is the process of checking user input before it is submitted to the server. We need form validation for the following reasons: - Data Integrity: Ensures that only correct and complete data is sent to the server (e.g., empty username or password fields are rejected). - Security: Pr...

Open this answer on its own page →

Single Page Application

2079 · 5 marks

What does single page application means? Discuss about server side strategies. [5]

A Single Page Application (SPA) is a web application that loads a single HTML page and dynamically updates the content as the user interacts with the application, without refreshing or reloading the entire page. Key characteristics: - Delivers dynamic updates to the user without full page reloads...

Open this answer on its own page →

Common client-side web technologies

2076 · 5 marks

Explain the client-side scripting with practical example.[5]

Client-side scripting refers to scripts that are executed on the client's browser rather than on the web server. The script is downloaded along with the HTML page and runs locally on the user's machine without requiring a round-trip to the server. JavaScript is the most widely used client-side sc...

Open this answer on its own page →

2076 · 5 marks

Differentiate between client-side and server-side script.[5]

Client-Side Script refers to code that is executed on the user's browser (client machine). The script is downloaded from the server and runs locally on the browser without any server interaction. Server-Side Script refers to code that is executed on the web server before the response is sent to t...

Open this answer on its own page →