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.
I don’t think that B is the right answer. In order to prevent from entering special characters,
the function should return false when a special character is entered.
And I’m also not sure if the change event is the most suitable for this job.
for preventing special chars you must check the char when key down ! your answer is wrong!
Answer should be C.
I agree with Rooky and hus. The answer should be C in order to stop specific characters from being entered.
i think is C answer to
i don’t think the keydown event works like this. just think about if someone just pastes text into the textbox, it would not trigger. a changed event would, so the keydown would be handled there.
when a change event returns false, it don’t change the value of the input, so change event can’t prevent users from entering specific characters.
https://jsfiddle.net/66c9e1Lf/
C is correct.
take this code, and try to paste anything usung mouse 🙂
C is the Correct Answer.
Why no d?
+++
Since nobody replied: the change event doesn’t contain keycodes, so you would never be able to determine which keycodes were pressed unless you convert the value of the input back to keycodes, which is weird and unlogical.
b and d are equal
b and d. both are ok.
A & C : error when users copy and paste using mouse
I’m so confused and still don’t know what the correct answer is…
I would say C (keydown) just because it’s the only event that actually prevents users from entering specific characters into the input field. (copying and pasting aside)
I think C is the right answer as well. Microsoft likes to be tricky with these so they try to catch you on the semantics. So in this question the key words are “entering specific characters”, it doesn’t specifically say pasting characters. So keydown is the MOST right answer.
hab answer is C