You develop an HTML5 webpage. You have the following HTML markup:
<input type=”text” id=”username” />
You need to prevent users from entering specific characters into the username field.
What should you do?
A.
Using the keyup event, add an anonymous function that returns true when a specific
character keycode value is determined.
B.
Using the change event, add an anonymous function that returns true when a specific
character keycode value is determined.
C.
Using the keydown event, add an anonymous function that returns false when a specific
character keycode value is determined.
D.
Using the change event, add an anonymous function that returns false when a specific
character keycode value is determined.
Explanation:
The answer is Key Down. https://jsfiddle.net/66c9e1Lf/
No, the answer is correct. Key Down event will not work if we paste value using mouse right click (from buffer). B is correct – http://stackoverflow.com/questions/3042120/in-javascript-event-handling-why-return-false-or-event-preventdefault-and
but change event does not prevent user input
Why D is not the answer?
Return False to prevent from entering specific chars?
yes, i think d is correct
Please explain. How would the code be in this case? Prove that this is right..
question says preventing, change event cannot prevent any char not being inserted via keyboard as is demonstrated in created example from Hawgie https://jsfiddle.net/66c9e1Lf/
but nothing is mention about coping and pasting – good to point this use case but i dont think should be addressed in any answer
C is correct