2082 9

CSC329 ยท 2082 ยท 5 marks

What is the use of alert box? Write a JavaScript program to show the use of RegExp object.

What is the use of alert box? Write a JavaScript program to show the use of RegExp object. [5]

Web TechnologyChapter 4: Client Side Scripting with JavaScriptBuilt-in Objects

Quick answer

An alert box is a built-in JavaScript dialog box used to display a message or information to the user. It shows a popup window with a message and an OK button. The user must click OK to proceed.

Alert Box and JavaScript RegExp Object

Alert Box

Uses of Alert Box:

  • To display important information or warnings to the user
  • To show validation error messages
  • To debug JavaScript code by displaying variable values
  • To notify the user about the result of an operation

Syntax:

alert("Your message here");

RegExp Object in JavaScript

A RegExp (Regular Expression) object is used to match character combinations in strings. It is used for pattern matching and text searching/replacing.

Syntax:

var pattern = new RegExp("pattern", "flags");
// OR shorthand
var pattern = /pattern/flags;

Common Flags:

FlagDescription
iCase-insensitive matching
gGlobal match (find all matches)
mMultiline matching

Common Methods:

MethodDescription
test()Returns true or false if pattern matches
exec()Returns matched result or null

JavaScript Program Using RegExp Object

<!DOCTYPE html>
<html>
<head>
    <title>RegExp Object Example</title>
</head>
<body>
    <h2>RegExp Object Demo</h2>

    <form>
        <label>Enter Username:</label>
        <input type="text" id="username" />  

        <label>Enter Password:</label>
        <input type="text" id="password" />  

        <input type="button" value="Validate" onclick="validate()" />
    </form>

    <script type="text/javascript">
        function validate() {
            var uname = document.getElementById("username").value;
            var pwd   = document.getElementById("password").value;

            // Username: only letters and digits, minimum 5 characters
            var unamePattern = new RegExp("^[a-zA-Z0-9]{5,}$");

            // Password: must start with a digit and end with $
            var pwdPattern = /^\d.*\$$/;

            // Validate username
            if (!unamePattern.test(uname)) {
                alert("Invalid username! Username must be at least 5 characters"
                      + " and contain only letters or digits.");
                return;
            }

            // Validate password
            if (!pwdPattern.test(pwd)) {
                alert("Invalid password! Password must start with a digit"
                      + " and end with '$'.");
                return;
            }

            alert("Validation Successful! Welcome, " + uname);
        }
    </script>
</body>
</html>

Explanation of Patterns Used

PatternMeaning
^[a-zA-Z0-9]{5,}$Username starts (^) and ends ($) with alphanumeric characters, minimum 5
^\d.*\$$Password starts (^) with a digit (\d), has any characters (.*), and ends with $
test()Returns true if the string matches the pattern, false otherwise

Note: The alert() box is used here to display validation messages, demonstrating both the use of alert boxes and the RegExp object together.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.