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.
input.valuedocument.inputinput.getValue()getElementById(input)getElementById()getElementsByTagName()getElementByName()document.formsdocument.querySelector("input[type='radio']:checked")document.getElementById("radio").checkedradio.value()document.forms.radiodocument.getElementsByTagName("input")document.forms[0].elementsform.getAllElements()document.getElementsByName()checkbox.checkedcheckbox.valuecheckbox.getElement()checkbox.isChecked()document.getElementById("id")document.forms.iddocument.querySelector("#id")document.querySelectorAll("input[name='checkboxName']")document.forms['formName'].checkboxNamedocument.getElementByName('checkboxName')document.getElementsByTagName('input')form.elements["email"].value?
submitButton.valuesubmitButton.checkedsubmitButton.getAttribute('value')submitButton.innerTextinput.getValue()document.getElementById('inputID').valueinput.value()document.querySelector('input').getValue()input.required()input.checkValidity()form.submit()input.isValid()input.value == ""input.value == nullinput.length == 0input.isEmpty()alert() to show messagesspan or div for the messageevent.preventDefault()form.stopSubmit()form.cancel()return falseinput.requiredinput.hasAttribute('required')input.isRequiredinput.isEmpty()form.submit()form.checkValidity()form.validate()form.submitValidation()<input required><input isRequired="true"><input validate="true"><input mandatory="true">input.isValid()input.checkValidity()input.valueCheck()input.validate()email.requiredemail.patterntype="email"email.isValidalert("Form invalid")message.innerHTML = "Invalid"showErrorMessage()form.invalidMessage()pattern attribute in HTML5 do?type="phone"pattern="^\d{3}-\d{3}-\d{4}$"required="true"type="text"type="text"type="email"type="password"type="url"maxlength attribute in HTML5 do?type="date"type="text"type="number"type="datetime-local"input:requiredinput.required<input required>input.isRequiredmin attribute in HTML5 input fields define?<input type="email"><input type="url"><input type="text"><input type="number">type="url"type="text"type="email"type="password"| Qno | Answer |
|---|---|
| 1 | A) input.value |
| 2 | D) document.forms |
| 3 | A) document.querySelector("input[type='radio']:checked") |
| 4 | B) document.forms[0].elements |
| 5 | A) checkbox.checked |
| 6 | D) Both A and C |
| 7 | A) document.querySelectorAll("input[name='checkboxName']") |
| 8 | B) The text inside the email input field |
| 9 | A) submitButton.value |
| 10 | B) document.getElementById('inputID').value |
| 11 | B) input.checkValidity() |
| 12 | A) input.value == "" |
| 13 | B) Insert a span or div for the message |
| 14 | A) event.preventDefault() |
| 15 | B) input.hasAttribute('required') |
| 16 | B) form.checkValidity() |
| 17 | A) <input required> |
| 18 | B) input.checkValidity() |
| 19 | C) type="email" |
| 20 | B) message.innerHTML = "Invalid" |
| 21 | A) Restricts the input to match a regular expression |
| 22 | B) pattern="^\d{3}-\d{3}-\d{4}$" |
| 23 | B) type="email" |
| 24 | A) Limits the number of characters a user can enter |
| 25 | A) type="date" |
| 26 | C) <input required> |
| 27 | C) The minimum value of numeric fields |
| 28 | C) <input type="text"> |
| 29 | C) The browser displays a built-in validation message |
| 30 | A) type="url" |