MCQs on Forms and Validation | JavaScript Intermediate

Forms are a core part of web development, and mastering form handling and validation is essential for building interactive websites. This chapter covers accessing form data, performing validation, and preventing unwanted submissions.


Working with Forms and Validation


Topic: Accessing Form Data

  1. How do you access the value of an input element in JavaScript?
    • A) input.value
    • B) document.input
    • C) input.getValue()
    • D) getElementById(input)
  2. Which method is used to access a form element by its name?
    • A) getElementById()
    • B) getElementsByTagName()
    • C) getElementByName()
    • D) document.forms
  3. How can you access the value of a radio button that is selected in a form?
    • A) document.querySelector("input[type='radio']:checked")
    • B) document.getElementById("radio").checked
    • C) radio.value()
    • D) document.forms.radio
  4. What is the correct way to retrieve all form elements in a form?
    • A) document.getElementsByTagName("input")
    • B) document.forms[0].elements
    • C) form.getAllElements()
    • D) document.getElementsByName()
  5. Which method is used to access the value of a checkbox input?
    • A) checkbox.checked
    • B) checkbox.value
    • C) checkbox.getElement()
    • D) checkbox.isChecked()
  6. How do you access a form element with a specific ID in JavaScript?
    • A) document.getElementById("id")
    • B) document.forms.id
    • C) document.querySelector("#id")
    • D) Both A and C
  7. Which of these methods is used to get the values of multiple checkboxes with the same name?
    • A) document.querySelectorAll("input[name='checkboxName']")
    • B) document.forms['formName'].checkboxName
    • C) document.getElementByName('checkboxName')
    • D) document.getElementsByTagName('input')
  8. What is returned when accessing form.elements["email"].value?
    • A) The email input field object
    • B) The text inside the email input field
    • C) The name of the input field
    • D) A boolean value
  9. How do you access a submit button value in a form?
    • A) submitButton.value
    • B) submitButton.checked
    • C) submitButton.getAttribute('value')
    • D) submitButton.innerText
  10. How do you get the value of a text input field using JavaScript?
    • A) input.getValue()
    • B) document.getElementById('inputID').value
    • C) input.value()
    • D) document.querySelector('input').getValue()

Topic: Simple Form Validation

  1. Which method is used to validate a required field in a form?
  • A) input.required()
  • B) input.checkValidity()
  • C) form.submit()
  • D) input.isValid()
  1. How can you check if a text input field is empty using JavaScript?
  • A) input.value == ""
  • B) input.value == null
  • C) input.length == 0
  • D) input.isEmpty()
  1. Which of the following is a common way to display error messages during form validation?
  • A) Use alert() to show messages
  • B) Insert a span or div for the message
  • C) Log the error in the console
  • D) Display an image for the error
  1. How do you stop a form from submitting when validation fails?
  • A) event.preventDefault()
  • B) form.stopSubmit()
  • C) form.cancel()
  • D) return false
  1. Which property is checked to see if a text input is required in HTML5?
  • A) input.required
  • B) input.hasAttribute('required')
  • C) input.isRequired
  • D) input.isEmpty()
  1. What is the correct way to trigger form validation?
  • A) form.submit()
  • B) form.checkValidity()
  • C) form.validate()
  • D) form.submitValidation()
  1. How can you make an input field required in HTML5?
  • A) <input required>
  • B) <input isRequired="true">
  • C) <input validate="true">
  • D) <input mandatory="true">
  1. What method can you use to check if a form input is valid?
  • A) input.isValid()
  • B) input.checkValidity()
  • C) input.valueCheck()
  • D) input.validate()
  1. Which of the following HTML attributes is used for validating email input?
  • A) email.required
  • B) email.pattern
  • C) type="email"
  • D) email.isValid
  1. How do you display a message if the form is invalid?
  • A) alert("Form invalid")
  • B) message.innerHTML = "Invalid"
  • C) showErrorMessage()
  • D) form.invalidMessage()

Topic: HTML5 Form Validation

  1. What does the pattern attribute in HTML5 do?
  • A) Restricts the input to match a regular expression
  • B) Makes the input field optional
  • C) Adds a placeholder to the input field
  • D) Validates a date format
  1. Which of the following attributes is used for validating a phone number in HTML5?
  • A) type="phone"
  • B) pattern="^\d{3}-\d{3}-\d{4}$"
  • C) required="true"
  • D) type="text"
  1. Which HTML5 input type ensures that only a valid email address can be entered?
  • A) type="text"
  • B) type="email"
  • C) type="password"
  • D) type="url"
  1. What does the maxlength attribute in HTML5 do?
  • A) Limits the number of characters a user can enter
  • B) Specifies the maximum width of the input box
  • C) Defines the maximum length of the label text
  • D) Restricts input to numeric values
  1. Which HTML5 input type validates that the user enters a date?
  • A) type="date"
  • B) type="text"
  • C) type="number"
  • D) type="datetime-local"
  1. How can you specify that a field is required in an HTML5 form?
  • A) input:required
  • B) input.required
  • C) <input required>
  • D) input.isRequired
  1. What does the min attribute in HTML5 input fields define?
  • A) The minimum number of characters allowed
  • B) The minimum length of the text field
  • C) The minimum value of numeric fields
  • D) The minimum time allowed in a date picker
  1. Which of these elements is NOT validated by default in HTML5?
  • A) <input type="email">
  • B) <input type="url">
  • C) <input type="text">
  • D) <input type="number">
  1. What happens if a user submits an invalid form with HTML5 validation?
  • A) The form is submitted with an error message
  • B) The form submits without validation
  • C) The browser displays a built-in validation message
  • D) The user is redirected to an error page
  1. Which HTML5 input type is used for validating URL entries?
  • A) type="url"
  • B) type="text"
  • C) type="email"
  • D) type="password"

Answers Table

QnoAnswer
1A) input.value
2D) document.forms
3A) document.querySelector("input[type='radio']:checked")
4B) document.forms[0].elements
5A) checkbox.checked
6D) Both A and C
7A) document.querySelectorAll("input[name='checkboxName']")
8B) The text inside the email input field
9A) submitButton.value
10B) document.getElementById('inputID').value
11B) input.checkValidity()
12A) input.value == ""
13B) Insert a span or div for the message
14A) event.preventDefault()
15B) input.hasAttribute('required')
16B) form.checkValidity()
17A) <input required>
18B) input.checkValidity()
19C) type="email"
20B) message.innerHTML = "Invalid"
21A) Restricts the input to match a regular expression
22B) pattern="^\d{3}-\d{3}-\d{4}$"
23B) type="email"
24A) Limits the number of characters a user can enter
25A) type="date"
26C) <input required>
27C) The minimum value of numeric fields
28C) <input type="text">
29C) The browser displays a built-in validation message
30A) type="url"

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top