Unit 4 of 4 · M.Sc IT Sem 2

Unit 4: Forms and JavaScript objects

Web Technologies notes · PTU syllabus (PGCA1909)

3 min read7 topics10 exam questions
On this page
  1. Unit summary
  2. The form object
  3. Text, password, button, submit and reset elements
  4. Checkboxes and radio buttons
  5. Textarea, select and multi-choice lists
  6. Form element methods and events
  7. Built-in objects: String, Math and Date
  8. User-defined objects and instances
  9. Key terms
  10. Quick revision
  11. Important questions

Unit summary

Forms collect user data and JavaScript objects process it. This unit covers the form object and its element methods — text, password, button, submit, reset, checkbox, radio, textarea, select and multi-choice lists — the built-in String, Math and Date objects, and user-defined objects and instances.

After this unit you can

  • Build forms with all element types
  • Access form elements and their methods in JavaScript
  • Use String, Math and Date objects
  • Create user-defined objects and instances

PTU syllabus topics

  • The form object and its element methods (text, password, button, submit, reset, checkbox, radio, textarea, select and multi-choice lists)
  • built-in objects (String, Math, Date)
  • user-defined objects and instances
ProcessValidating a form with JavaScript
  1. 1User clicks Submit
  2. 2onsubmit calls a function
  3. 3Check each field

    Empty, length, format

  4. 4If invalid

    Show message, return false

  5. 5If valid

    Form is submitted

1

Topic 1

The form object

html<form action="register.php" method="post">
  ...controls...
</form>
  • action: where the data is sent (a server script).
  • method: GET sends data in the URL (visible, limited length, for searches); POST sends it in the request body (hidden, for passwords and large data).
ComparisonGET vs POST
GET
POST

Data location

In the URL

In the request body

Visibility

Visible

Not visible in the URL

Size

Limited

Large

Use

Search, filters

Login, registration, uploads

2

Topic 2

Text, password, button, submit and reset elements

ControlCodePurpose
Text box<input type="text" name="uname">Single line of text
Password<input type="password" name="pwd">Hides typed characters
Submit<input type="submit" value="Register">Sends the form
Reset<input type="reset">Clears all fields
Button<input type="button" value="Check" onclick="check()">Runs a script

Useful attributes: name (identifies the data), value, placeholder, maxlength, size and required.

3

Topic 3

Checkboxes and radio buttons

  • Checkbox: choose any number of options. <input type="checkbox" name="skills" value="C++"> C++
  • Radio button: choose only one option from a group; all buttons in the group share the same name. <input type="radio" name="gender" value="F"> Female
  • checked pre-selects an option.

Exam tip

Radio buttons in a group must have the same name — otherwise more than one can be selected. This is a common practical mistake.

4

Topic 4

Textarea, select and multi-choice lists

  • Textarea: multi-line text such as an address: <textarea name="address" rows="4" cols="40"></textarea>
  • Select (drop-down) list:
html<select name="course">
  <option value="bca">BCA</option>
  <option value="bba" selected>BBA</option>
</select>
  • Multi-choice select list: add multiple (and often size) so the user can select several options with Ctrl.
ClassificationForm controls at a glance
Form
  • Text input

    Name, email

  • Password

    Hidden typing

  • Radio

    One of many

  • Checkbox

    Many of many

  • Select

    Drop-down choice

  • Textarea

    Long text

  • Submit and reset

    Send or clear

5

Topic 5

Form element methods and events

An event is something that happens on the page: click, change, submit, load, mouseover, keyup. JavaScript can respond to it.

html<form name="login" onsubmit="return validate()">
  <input name="user"> <input type="password" name="pwd">
  <input type="submit" value="Login">
</form>
<script>
function validate() {
  const f = document.forms["login"];
  if (f.user.value === "") { alert("Enter username"); return false; }
  if (f.pwd.value.length < 6) { alert("Password too short"); return false; }
  return true;
}
</script>

Returning false from the submit handler stops the form from being sent.

6

Topic 6

Built-in objects: String, Math and Date

ComparisonBuilt-in tools
Example
Result

String methods

"SBS College".toUpperCase()

"SBS COLLEGE"

Array methods

[3,1,2].sort()

[1,2,3]

Date

new Date().getFullYear()

Current year

Math

Math.round(Math.random() × 10)

Random 0–10

JSON

JSON.stringify(obj) and JSON.parse(text)

Object ↔ text

  • Objects: let student = { name: "Ana", roll: 12 }; student.name;
  • Array methods: push, pop, shift, slice, splice, indexOf, map, filter, forEach, join.
  • JSON (JavaScript Object Notation) is a text format for exchanging data with servers.
7

Topic 7

User-defined objects and instances

javascript// Object literal
const student = { name: "Aman", marks: 82, grade() { return this.marks >= 75 ? "A" : "B"; } };

// Constructor function and instances
function Book(title, price) {
  this.title = title;
  this.price = price;
  this.discount = function (pct) { return this.price * (1 - pct / 100); };
}
const b1 = new Book("C Programming", 450);
const b2 = new Book("Web Design", 600);
console.log(b1.discount(10));   // 405

// ES6 class
class Account {
  constructor(owner, balance) { this.owner = owner; this.balance = balance; }
  deposit(amt) { this.balance += amt; return this.balance; }
}

Key terms

Form object
JavaScript representation of an HTML form
Radio button
Option allowing one choice in a group
Math object
Built-in object with mathematical functions
Constructor function
Function creating objects with new
Instance
Object created from a constructor or class

Quick revision

  • form action and method (GET, POST).
  • text, password, button, submit, reset, checkbox, radio, textarea, select, multiple.
  • value, checked, selectedIndex; focus, blur, submit, reset; validation.
  • String, Math and Date methods.
  • Object literals, constructor functions, classes, new.

Important exam questions

Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).

Short-answer questions

  1. Q1.Distinguish GET and POST.
  2. Q2.Distinguish checkbox and radio button.
  3. Q3.How do you allow multiple selections in a list?
  4. Q4.What does Math.random() return?
  5. Q5.How do you get the current year in JavaScript?
  6. Q6.What is the new keyword used for?

Long-answer questions

  1. Q1.Design a registration form using all form elements.
  2. Q2.Explain form element methods and validation with JavaScript.
  3. Q3.Explain String, Math and Date objects with examples.
  4. Q4.Explain user-defined objects and instances in JavaScript.

Stuck on this unit?

Message SBS on WhatsApp for help with Web Technologies, or to ask about studying M.Sc IT at Synetic.

WhatsApp us