MCQs Of Day 24 :JavaScript and Forms

Rashmi Mishra
0

 MCQs Of Day 24

JavaScript and Forms

Section 1: Basics of JavaScript and Forms

1.   What does the getElementById method do?

o    A) Finds all elements by class name

o    B) Finds an element by its ID

o    C) Finds all elements by tag name

o    D) Finds elements by their type
Answer: B
Explanation: getElementById is used to access a single element with a specific ID.

2.   Which event is commonly used to trigger form validation before submission?

o    A) onmouseover

o    B) onsubmit

o    C) onchange

o    D) onreset
Answer: B
Explanation: The onsubmit event is fired when a form is submitted, making it ideal for validation.

3.   Which method prevents the default action of a form submission?

o    A) stopPropagation()

o    B) preventDefault()

o    C) haltEvent()

o    D) stopDefault()
Answer: B
Explanation: preventDefault() prevents the browser’s default behavior, such as submitting the form.

4.   What property of an input element retrieves its value?

o    A) text

o    B) innerHTML

o    C) value

o    D) content
Answer: C
Explanation: The value property is used to retrieve or set the value of an input element.

5.   Which of the following is a valid JavaScript way to select all input fields in a form?

o    A) document.querySelectorAll("input")

o    B) document.getElementsByClassName("input")

o    C) document.getInputByType("all")

o    D) document.inputSelector("all")
Answer: A
Explanation: querySelectorAll selects all elements matching the given selector.


Section 2: Accessing Form Elements

6.   Which property accesses the form element's name attribute?

o    A) form.name

o    B) form[0]

o    C) form["name"]

o    D) form.elements["name"]
Answer: D
Explanation: elements provides a collection of all form controls, accessible by their name.

7.   How can you access the first form on a webpage?

o    A) document.forms[1]

o    B) document.forms.first()

o    C) document.forms[0]

o    D) document.getFirstForm()
Answer: C
Explanation: document.forms returns an array-like collection of all forms, starting at index 0.

8.   What does form.elements.length return?

o    A) The total number of forms on the page

o    B) The total number of elements in the form

o    C) The total number of input fields on the page

o    D) The number of submit buttons in the form
Answer: B
Explanation: The elements collection contains all elements of a form, and length returns their count.


Section 3: Validating Form Input

9.   Which JavaScript function checks if a field is empty?

o    A) isEmptyField()

o    B) input.trim() === ""

o    C) field.clear()

o    D) field.isEmpty()
Answer: B
Explanation: Trimming whitespace and comparing to an empty string ensures no characters are present.

10.                     Which method can check if an email input value is valid?

o    A) isEmail()

o    B) test() with a regex

o    C) validateEmail()

o    D) checkInput("email")
Answer: B
Explanation: Regular expressions (regex) are commonly used to validate email format.

11.                     What property checks the validity of a form element?

o    A) checkValidity()

o    B) isValid()

o    C) validate()

o    D) form.valid
Answer: A
Explanation: checkValidity() is a built-in method for form validation.


Section 4: Preventing Form Submission

12.                     How do you stop a form from submitting?

o    A) Call return false inside the onsubmit handler

o    B) Use stopSubmit()

o    C) Remove the action attribute

o    D) Use form.preventSubmit()
Answer: A
Explanation: Returning false in the onsubmit event handler stops submission.

13.                     Which of the following prevents default behavior?

o    A) form.reset()

o    B) form.validate()

o    C) event.preventDefault()

o    D) stopPropagation()
Answer: C
Explanation: event.preventDefault() is specifically designed to stop the default action.


Section 5: Real-World Scenarios

14.                     How can you enable or disable a submit button based on input validation?

o    A) By toggling the readonly property of the button

o    B) By setting button.disabled to true or false

o    C) By hiding the button element using CSS

o    D) By using form.hideSubmit()
Answer: B
Explanation: The disabled property of a button can be dynamically set.

15.                     What does pattern attribute in input fields do?

o    A) Specifies a regex for validation

o    B) Limits the number of characters

o    C) Specifies placeholder text

o    D) Sets a default value
Answer: A
Explanation: The pattern attribute allows you to define regex for input validation.

16.                     How would you reset all form fields in JavaScript?

o    A) form.clear()

o    B) form.elements.reset()

o    C) form.reset()

o    D) document.clearForm()
Answer: C
Explanation: The reset() method clears all user input in a form.


Section 6: Dynamic Feedback

17.                     Which event is suitable for providing real-time feedback while a user types?

o    A) onchange

o    B) onblur

o    C) onkeyup

o    D) onsubmit
Answer: C
Explanation: The onkeyup event is triggered every time the user releases a key, enabling real-time feedback.

18.                     How can you display an error message next to an invalid field?

o    A) Use alert()

o    B) Add an error message to a sibling element using JavaScript

o    C) Redirect to another page

o    D) Use the browser’s default error handling
Answer: B
Explanation: Dynamically updating a sibling element is a user-friendly way to show errors.


These are the first 18 MCQs, organized into meaningful sections. Let me know if you'd like the remaining questions structured similarly, or if you have preferences for specific subtopics within JavaScript and Forms!

4o

You said:

yes

ChatGPT said:

ChatGPT

Here are more MCQs for JavaScript and Forms, continuing from where we left off:


Section 7: Advanced Accessing Form Elements

19.                     Which method retrieves form data from a form element in JSON format?

o    A) JSON.stringify(form)

o    B) new FormData(form)

o    C) form.toJSON()

o    D) document.serialize(form)
Answer: B
Explanation: FormData is used to collect data from forms, which can be converted to JSON using custom logic.

20.                     How do you get the value of a selected option in a dropdown?

o    A) dropdown.selectedIndex

o    B) dropdown.options[dropdown.selectedIndex].value

o    C) dropdown.text

o    D) dropdown.selectedOption
Answer: B
Explanation: This accesses the options array of the dropdown and retrieves the value of the selected option.

21.                     What property identifies whether a checkbox is checked?

o    A) checkbox.selected

o    B) checkbox.isChecked

o    C) checkbox.checked

o    D) checkbox.status
Answer: C
Explanation: The checked property of a checkbox element determines its state.

22.                     How do you access the action URL of a form in JavaScript?

o    A) form.getAttribute('action')

o    B) form.action

o    C) document.getFormAction()

o    D) form.url
Answer: B
Explanation: The action property of a form element contains the URL where the form data is submitted.

23.                     What happens if you don’t set a method attribute on a form?

o    A) Form submission fails

o    B) The method defaults to POST

o    C) The method defaults to GET

o    D) The form uses an invalid method
Answer: C
Explanation: If the method attribute is not specified, it defaults to GET.


Section 8: Form Validation with JavaScript

24.                     Which method ensures a user enters a value in a required field?

o    A) Add the required attribute in HTML

o    B) Use if (input.value !== "") in JavaScript

o    C) Use input.checkValidity() in JavaScript

o    D) All of the above
Answer: D
Explanation: Any of these methods can ensure a required field is filled.

25.                     What does the novalidate attribute in a <form> tag do?

o    A) Enables HTML5 validation

o    B) Disables browser-based validation

o    C) Clears form inputs after submission

o    D) Validates the form dynamically
Answer: B
Explanation: The novalidate attribute prevents the browser from performing default HTML5 form validation.

26.                     What is the correct way to validate a number input field?

o    A) Use the type="number" attribute in HTML

o    B) Use JavaScript to check if Number(input.value) is valid

o    C) Use a regex to validate the input

o    D) All of the above
Answer: D
Explanation: All these methods are valid approaches for validating numeric input.

27.                     How can you set a custom error message for an invalid field in JavaScript?

o    A) input.error = "Error message"

o    B) input.setCustomValidity("Error message")

o    C) input.validateMessage = "Error message"

o    D) input.checkValidity("Error message")
Answer: B
Explanation: The setCustomValidity() method allows you to define a custom error message.

28.                     What does the maxlength attribute do in an input field?

o    A) Restricts the number of characters that can be entered

o    B) Validates the input length dynamically

o    C) Removes extra characters automatically

o    D) Limits the output of the input
Answer: A
Explanation: The maxlength attribute ensures users cannot input more than the specified number of characters.


Section 9: Preventing Form Submission

29.                     How can you ensure a form submits only if all fields are valid?

o    A) Add return false to invalid fields

o    B) Use event.preventDefault() on invalid fields

o    C) Check each field's validity using checkValidity() before allowing submission

o    D) Use CSS to highlight invalid fields
Answer: C
Explanation: Using checkValidity() ensures only valid inputs are submitted.

30.                     Which of the following can prevent form submission?

o    A) JavaScript logic in the onsubmit handler

o    B) Using event.preventDefault()

o    C) Returning false in the submit handler

o    D) All of the above
Answer: D
Explanation: All these methods can stop form submission based on conditions.

31.                     What happens when you prevent the default submission behavior of a form?

o    A) The form reloads the page

o    B) The form clears all inputs

o    C) The browser doesn’t send form data to the server

o    D) The form action URL is executed without sending data
Answer: C
Explanation: Preventing default submission stops the browser from sending data to the server.


Section 10: Real-World Scenarios

32.                     What is the best way to validate a password field?

o    A) Check for a minimum length using minlength

o    B) Use regex to enforce a combination of characters

o    C) Use JavaScript to match against required rules

o    D) All of the above
Answer: D
Explanation: Combining all these methods ensures robust password validation.

33.                     How do you dynamically change an input field to readonly?

o    A) input.readOnly = true

o    B) input.setAttribute('readonly', true)

o    C) input.disable()

o    D) A and B
Answer: D
Explanation: Both readOnly and setAttribute can make a field readonly dynamically.

34.                     Which attribute ensures a dropdown always starts with a default option?

o    A) default

o    B) selected

o    C) placeholder

o    D) value
Answer: B
Explanation: The selected attribute sets the default selected option in a dropdown.

35.                     How would you style invalid fields dynamically?

o    A) Add an invalid class using JavaScript

o    B) Use the :invalid CSS pseudo-class

o    C) Highlight using inline styles

o    D) All of the above
Answer: D
Explanation: These approaches can be combined to visually indicate invalid fields.


Continuation

Let me know if you'd like me to keep adding more or tailor the questions toward a specific subtopic!

4o

You said:

yes

ChatGPT said:

ChatGPT

Here’s a continuation of MCQs on JavaScript and Forms:


Section 11: Advanced Form Validation

36.                     Which method is best to validate an email field in JavaScript?

o    A) Use type="email" in the HTML input tag

o    B) Use a regex pattern to match email formats

o    C) Use the checkValidity() method

o    D) All of the above
Answer: D
Explanation: All these methods can validate email input; combining them ensures more robust validation.

37.                     What does the pattern attribute do in an HTML form input?

o    A) Restricts input to match a specified regular expression

o    B) Formats the input dynamically

o    C) Ensures only numbers are entered

o    D) Prevents empty inputs
Answer: A
Explanation: The pattern attribute validates the input against a provided regular expression.

38.                     What will input.validity.valid return for an invalid required input field?

o    A) true

o    B) false

o    C) null

o    D) undefined
Answer: B
Explanation: The validity.valid property returns false if the field is invalid.

39.                     Which of the following is NOT a part of the validity property in JavaScript?

o    A) valueMissing

o    B) typeMismatch

o    C) minLengthError

o    D) patternMismatch
Answer: C
Explanation: The minLengthError is not a predefined property of the validity object.

40.                     How do you validate the length of a string entered into a text field?

o    A) Use the maxlength attribute in HTML

o    B) Use JavaScript to check input.value.length

o    C) Combine HTML and JavaScript validation

o    D) All of the above
Answer: D
Explanation: Both HTML attributes and JavaScript can be used to validate input length effectively.


Section 12: Preventing Form Submission - Advanced

41.                     Which JavaScript event is fired during a form submission?

o    A) onchange

o    B) onsubmit

o    C) onload

o    D) onclick
Answer: B
Explanation: The onsubmit event triggers when the form is submitted.

42.                     What is the purpose of the return false statement in a form submit handler?

o    A) Stops JavaScript execution

o    B) Prevents form submission

o    C) Validates the form

o    D) Clears form inputs
Answer: B
Explanation: Returning false in the submit handler stops the default submission action.

43.                     Which method allows validation to occur before submission?

o    A) Adding validation logic inside the onsubmit event

o    B) Using form.onvalidate

o    C) Triggering input.validate()

o    D) Adding return true in the handler
Answer: A
Explanation: Adding logic inside the onsubmit event ensures validation happens before submission.

44.                     Which of these does NOT prevent form submission?

o    A) event.preventDefault()

o    B) Returning false from a submit handler

o    C) An input field with readonly attribute

o    D) Adding validation conditions in the onsubmit event
Answer: C
Explanation: The readonly attribute disables editing but does not stop submission.

45.                     How do you prevent submission when the form is invalid?

o    A) Add onsubmit="return validateForm()"

o    B) Use event.preventDefault()

o    C) Both A and B

o    D) Set form.submit = false
Answer: C
Explanation: Combining return validateForm() and event.preventDefault() is a robust way to prevent invalid form submission.


Section 13: Real-World Scenarios

46.                     What is the best way to display error messages for invalid fields dynamically?

o    A) Use alert()

o    B) Create a <span> or <div> for error messages and update its content

o    C) Log errors in the console

o    D) Change the field’s background color only
Answer: B
Explanation: Dynamically updating the DOM to show error messages provides a better user experience.

47.                     How do you clear all inputs in a form using JavaScript?

o    A) form.clear()

o    B) form.reset()

o    C) form.elements.reset()

o    D) form.clearInputs()
Answer: B
Explanation: The reset() method clears all input fields to their initial values.

48.                     What attribute would you use to allow multiple files to be selected in a file input?

o    A) multi

o    B) multiple

o    C) allow-multi

o    D) file-multiple
Answer: B
Explanation: The multiple attribute allows users to select multiple files.

49.                     Which method allows retrieving all selected options in a multi-select dropdown?

o    A) select.options

o    B) Array.from(select.selectedOptions)

o    C) select.getSelected()

o    D) select.selected
Answer: B
Explanation: The selectedOptions property contains a collection of all selected options.

50.                     How would you validate that two password fields match before submission?

o    A) Compare their value properties in JavaScript

o    B) Use the pattern attribute in HTML

o    C) Add type="password-match" to the second field

o    D) Use regex to compare both fields
Answer: A
Explanation: Comparing the value of both fields in JavaScript ensures they match.


Let me know if you'd like me to continue or expand further!

4o

You said:

yes

ChatGPT said:

ChatGPT

Here’s a continuation of MCQs on JavaScript and Forms:


Section 14: Accessing Form Elements

51.                     What property is used to access the value of an input field in JavaScript?

o    A) input.text

o    B) input.value

o    C) input.content

o    D) input.data
Answer: B
Explanation: The value property of an input element provides or sets the current value of the field.

52.                     What does the document.forms property return?

o    A) A list of all form values

o    B) A collection of all form elements in the document

o    C) An array of input fields in a form

o    D) The default form submission URL
Answer: B
Explanation: The document.forms property returns a collection of all <form> elements in the document.

53.                     How do you access an element in a form by its name attribute?

o    A) form.getElementByName()

o    B) form[name]

o    C) form.elements[name]

o    D) form.querySelector(name)
Answer: C
Explanation: You can access an element by its name using the form.elements[name] syntax.

54.                     Which method retrieves the value of a selected radio button in a form?

o    A) form.getValue(radio)

o    B) Loop through all radio buttons and check checked property

o    C) radioGroup.value

o    D) Use the radioGroup.getSelected() method
Answer: B
Explanation: To retrieve the value of a selected radio button, loop through the group and check which one is checked.

55.                     What happens if form.submit() is called directly in JavaScript?

o    A) The form validation is ignored

o    B) Validation runs as usual

o    C) The form resets

o    D) The browser displays a warning
Answer: A
Explanation: Calling form.submit() bypasses built-in validation and submits the form directly.


Section 15: Common Errors and Debugging

56.                     What error occurs when trying to access a non-existent form field?

o    A) ReferenceError

o    B) TypeError

o    C) UndefinedError

o    D) It returns undefined without an error
Answer: D
Explanation: Accessing a non-existent form field returns undefined but does not throw an error.

57.                     What will document.getElementById("form1").submit(); do if form1 has invalid fields?

o    A) Prevent submission until errors are resolved

o    B) Submit the form without validation

o    C) Throw a validation error

o    D) Log the validation errors in the console
Answer: B
Explanation: The submit() method ignores validation and submits the form.

58.                     Which console log helps in debugging form validation?

o    A) console.error(form.errors)

o    B) console.log(input.validity)

o    C) console.debug(input.errors)

o    D) console.warn(form.validation)
Answer: B
Explanation: Logging the validity object helps to identify why a field is invalid.

59.                     What does a patternMismatch validity state indicate?

o    A) The input does not match the required regex pattern

o    B) The input length exceeds the maximum allowed

o    C) The input is empty

o    D) The field type is incorrect
Answer: A
Explanation: The patternMismatch state is triggered when input does not match the pattern regex.

60.                     What should be done if a form validation script is not working?

o    A) Use alert() for debugging

o    B) Check if onsubmit is correctly assigned

o    C) Ensure event listeners are attached after the DOM loads

o    D) All of the above
Answer: D
Explanation: Debugging steps include checking script logic, event attachment, and browser console.


Section 16: Cross-Browser Compatibility

61.                     Which validation method is NOT supported by older browsers?

o    A) HTML5 attributes like required

o    B) JavaScript validation scripts

o    C) addEventListener for form events

o    D) DOM manipulation
Answer: A
Explanation: HTML5 form attributes like required may not work in outdated browsers.

62.                     What should you use for backward compatibility with HTML5 form validation?

o    A) Fallback JavaScript validation scripts

o    B) Ignore validation for old browsers

o    C) Rely on server-side validation only

o    D) Use custom form libraries
Answer: A
Explanation: JavaScript can be used as a fallback for browsers that do not support HTML5 validation.

63.                     Which method ensures consistent validation across all browsers?

o    A) HTML5 validation only

o    B) JavaScript-only validation

o    C) Combining HTML5 and JavaScript validation

o    D) Relying on server-side validation
Answer: C
Explanation: Combining both methods ensures consistent user experience and robust validation.

64.                     How do polyfills help with form validation?

o    A) They add custom validation styles

o    B) They mimic missing features in older browsers

o    C) They simplify HTML code

o    D) They validate forms on the server
Answer: B
Explanation: Polyfills emulate missing browser features to support modern functionality.

65.                     What is the purpose of using novalidate on a form?

o    A) Disables default HTML5 validation

o    B) Allows JavaScript to handle all validation

o    C) Prevents form submission

o    D) Both A and B
Answer: D
Explanation: The novalidate attribute disables HTML5 validation, enabling custom JavaScript validation.


Would you like more MCQs or a focus on specific subtopics?

4o

You said:

yes

ChatGPT said:

ChatGPT

Here’s a continuation with more MCQs focused on JavaScript and Forms:


Section 17: Preventing Form Submission

66.                     Which of the following can be used to prevent a form from submitting when a button is clicked?

o    A) event.preventDefault()

o    B) form.preventSubmit()

o    C) return false;

o    D) Both A and C
Answer: D
Explanation: Both event.preventDefault() and return false; prevent the default form submission behavior.

67.                     What is the purpose of event.preventDefault() in form validation?

o    A) To cancel the form submission if there are validation errors

o    B) To submit the form asynchronously

o    C) To prevent the page from reloading

o    D) To display a success message
Answer: A
Explanation: event.preventDefault() is used to stop form submission if validation fails.

68.                     How can form submission be triggered manually after validation passes?

o    A) form.submit()

o    B) form.submitForm()

o    C) form.dispatchEvent(new Event('submit'))

o    D) Both A and C
Answer: D
Explanation: form.submit() or triggering a custom submit event can manually submit the form after validation.

69.                     If you have an onsubmit event handler in JavaScript, what is the return value of the handler if you want to allow the form to submit?

o    A) false

o    B) undefined

o    C) true

o    D) event.preventDefault()
Answer: C
Explanation: Returning true from the onsubmit handler allows the form to submit. Returning false prevents it.

70.                     Which JavaScript event should be used to capture form submission before it happens?

o    A) onclick

o    B) onsubmit

o    C) oninput

o    D) onchange
Answer: B
Explanation: The onsubmit event is triggered when the user submits the form, allowing you to intervene before submission.


Section 18: Form Validation Using JavaScript

71.                     Which attribute is used to specify the minimum number of characters for a text input?

o    A) minlength

o    B) min-length

o    C) min

o    D) length
Answer: A
Explanation: The minlength attribute specifies the minimum number of characters required in an input field.

72.                     How can you check if a text input is empty using JavaScript?

o    A) if (input.value == '')

o    B) if (input == '')

o    C) if (input.value == null)

o    D) if (input == null)
Answer: A
Explanation: Checking input.value == '' verifies if the input field is empty.

73.                     Which method can you use to check if a checkbox is checked in JavaScript?

o    A) input.checked()

o    B) input.isChecked()

o    C) input.checked

o    D) input.selected
Answer: C
Explanation: The checked property of a checkbox element returns true if the checkbox is selected.

74.                     How do you make sure that a radio button group has been selected?

o    A) Check input.checked for each radio button

o    B) Use input.selected for the group

o    C) Use input.isSelected() method

o    D) Use input.value of the first radio button
Answer: A
Explanation: To validate that a radio button group is selected, you must check the checked property of each button in the group.

75.                     Which of the following is the correct way to validate a form using required attribute in HTML5?

o    A) Add required to the input field in the HTML

o    B) Add required to the input field using JavaScript

o    C) Use input.required == true in JavaScript

o    D) Use input.checkValidity() in JavaScript
Answer: A
Explanation: The required attribute in HTML5 makes the input field mandatory to fill out before submission.


Section 19: Handling Form Inputs

76.                     What does the input.type property return?

o    A) The current value of the input

o    B) The type of the input element (e.g., text, checkbox)

o    C) The name of the input field

o    D) The default value of the input
Answer: B
Explanation: The input.type property returns the type of the input element (e.g., text, number, radio, checkbox).

77.                     What is the purpose of the input.setCustomValidity() method?

o    A) To reset form validation

o    B) To add a custom error message when the input is invalid

o    C) To clear the form input value

o    D) To set the input type
Answer: B
Explanation: The setCustomValidity() method is used to set a custom error message for invalid input.

78.                     Which method checks whether the input value is valid according to its type and constraints?

o    A) input.checkValidity()

o    B) input.isValid()

o    C) input.valid()

o    D) input.validate()
Answer: A
Explanation: The checkValidity() method checks whether the input satisfies the validation constraints like required, minlength, etc.

79.                     Which JavaScript method is used to get the selected option from a <select> dropdown?

o    A) select.getSelectedOption()

o    B) select.selected

o    C) select.selectedIndex

o    D) select.selectedOption
Answer: C
Explanation: The selectedIndex property returns the index of the selected option in a <select> dropdown.

80.                     Which JavaScript function is used to get the value of a <textarea> element?

o    A) textarea.value()

o    B) textarea.text()

o    C) textarea.getValue()

o    D) textarea.value
Answer: D
Explanation: The value property is used to get or set the content of a <textarea> element.


Section 20: Preventing Duplicate Submissions

81.                     Which of the following is the best way to prevent duplicate form submissions?

o    A) Disable the submit button after the first click

o    B) Use submit.prevent() method

o    C) Set a timeout before the second submission

o    D) Automatically reset the form after submission
Answer: A
Explanation: Disabling the submit button prevents the user from clicking it multiple times before the form is processed.

82.                     What happens if a form is submitted and the submit button is clicked twice?

o    A) The form will submit twice

o    B) The form will show an error

o    C) The second click will be ignored if the button is disabled

o    D) It will reset the form
Answer: C
Explanation: Disabling the submit button after the first click ensures that the form is only submitted once.

83.                     What would be a good approach to prevent accidental form submission by pressing the Enter key?

o    A) Disable the Enter key on the form

o    B) Use event.preventDefault() on the form's submit event

o    C) Use input.removeAttribute('required')

o    D) Use input.blur() when the Enter key is pressed
Answer: B
Explanation: Preventing the form submission with event.preventDefault() on submit ensures that accidental submissions are avoided.


Would you like more questions on any specific topic or section?

4o mini

You said:

no ...from same above topic

ChatGPT said:

ChatGPT

Certainly! Here are additional MCQs focused on JavaScript and Forms, continuing from where we left off:


Section 21: Working with Form Elements

84.                     Which of the following methods can be used to access an individual form element by its name attribute?

o    A) document.getElementByName('elementName')

o    B) document.getElementById('elementName')

o    C) document.forms['formName']['elementName']

o    D) document.querySelector('#elementName')
Answer: C
Explanation: The correct way to access an individual form element by its name is document.forms['formName']['elementName'].

85.                     How can you retrieve the value of a radio button group in JavaScript?

o    A) document.getElementByName('groupName').value

o    B) document.getElementById('groupName').value

o    C) document.querySelectorAll('input[name="groupName"]')

o    D) document.getElementById('groupName').checked
Answer: A
Explanation: To get the value of the selected radio button from a group, use document.getElementByName('groupName').value.

86.                     What will the reset() method do to a form in JavaScript?

o    A) Reset the form values to their default state

o    B) Submit the form automatically

o    C) Trigger a custom validation

o    D) Re-render the form HTML
Answer: A
Explanation: The reset() method will reset the form fields to their initial default values.

87.                     How can you access the value of a checkbox in JavaScript?

o    A) checkbox.checked

o    B) checkbox.value

o    C) checkbox.isSelected()

o    D) checkbox.status
Answer: A
Explanation: The checked property of a checkbox returns true if it is checked, otherwise false.

88.                     What is the return type of the document.forms property in JavaScript?

o    A) HTMLFormElement

o    B) HTMLCollection

o    C) NodeList

o    D) Array
Answer: B
Explanation: The document.forms property returns an HTMLCollection of all the form elements in the document.


Section 22: Form Validation and Error Handling

89.                     What is the role of input.setCustomValidity() in form validation?

o    A) It triggers the validation process

o    B) It prevents form submission

o    C) It displays a custom error message

o    D) It removes the input field from validation
Answer: C
Explanation: The setCustomValidity() method sets a custom error message for form validation.

90.                     Which event handler is used to check form validation before submission in JavaScript?

o    A) onchange

o    B) onsubmit

o    C) onblur

o    D) oninput
Answer: B
Explanation: The onsubmit event is used to handle form validation before submission.

91.                     How can you check if a form input is valid using JavaScript?

o    A) input.isValid()

o    B) input.checkValidity()

o    C) input.validity()

o    D) input.validate()
Answer: B
Explanation: The checkValidity() method returns true if the input meets all validation constraints, otherwise false.

92.                     Which of the following methods can be used to reset the form's validation state?

o    A) form.resetValidation()

o    B) form.clear()

o    C) form.reset()

o    D) form.resetForm()
Answer: C
Explanation: The reset() method resets all form fields and clears any validation messages.

93.                     What is the purpose of the input.validity property?

o    A) To check if the input value is valid according to its constraints

o    B) To set the validity state of the input

o    C) To trigger the validation process manually

o    D) To set a custom error message for invalid input
Answer: A
Explanation: The validity property is used to access the input's validity state (e.g., valid, tooShort, typeMismatch, etc.).


Section 23: Custom Validation Messages

94.                     What should be the return value of a custom validation function to indicate a valid input?

o    A) true

o    B) false

o    C) null

o    D) An empty string ""
Answer: A
Explanation: Returning true from a custom validation function indicates that the input is valid.

95.                     Which method is used to add a custom error message for a form element?

o    A) setCustomMessage()

o    B) setCustomValidity()

o    C) setErrorMessage()

o    D) showError()
Answer: B
Explanation: The setCustomValidity() method is used to set a custom error message for an input element.

96.                     Which of the following is true about the input.validity property?

o    A) It can only be checked on textarea inputs

o    B) It contains boolean values indicating validation status

o    C) It contains an object with different error types for invalid input

o    D) It is read-only and cannot be modified
Answer: C
Explanation: The validity property is an object that contains various error types for invalid input (e.g., valueMissing, typeMismatch).


Section 24: Form Submission and Processing

97.                     Which method is used to manually submit a form in JavaScript after validation?

o    A) form.submit()

o    B) form.send()

o    C) form.trigger()

o    D) form.submitForm()
Answer: A
Explanation: The submit() method is used to manually submit a form after all validations are passed.

98.                     How can you prevent the default action of form submission in JavaScript?

o    A) event.preventDefault()

o    B) event.stopPropagation()

o    C) form.prevent()

o    D) form.stop()
Answer: A
Explanation: The event.preventDefault() method prevents the default form submission action.

99.                     If you want to check the validity of all the form inputs before submission, which method would you use?

o    A) form.validity()

o    B) form.checkValidity()

o    C) form.isValid()

o    D) form.validate()
Answer: B
Explanation: The checkValidity() method checks the validity of all form inputs and returns true if all inputs are valid.

100.               Which of the following attributes can be used to trigger form validation when the user submits the form?
- A) validate
- B) validation
- C) novalidate
- D) submitValidation
Answer: C
Explanation: The novalidate attribute is used to disable automatic form validation on form submission.

Post a Comment

0Comments

Post a Comment (0)