Programming in HTML5 with JavaScript and CSS3 — Question 54
You are developing a customer web form that includes the following HTML.
<input id = "txtValue" />
A customer must enter a value in the text box prior to submitting the form.
You need to add validation to the text box control.
Which HTML should you use?
Answer options
- A. <input id="txtValue" type="text" required="required"/>
- B. <input id="txtValue" type="text" pattern="[A-Za-z]{3}" />
- C. <input id="txtValue" type="required" />
- D. <input id="txtValue" type="required" autocomplete="on" />
Correct answer: A
Explanation
The correct answer is A because it uses the 'required' attribute, ensuring that the text box must be filled out before the form can be submitted. Option B only enforces a specific pattern and does not require an entry. Options C and D incorrectly use 'type' for 'required', which is not a valid input type, thus failing to provide the necessary validation.