You need to prevent users from entering specific characters into the username field

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?

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.



Leave a Reply 18

Your email address will not be published. Required fields are marked *


d

d

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.

Rooky

Rooky

for preventing special chars you must check the char when key down ! your answer is wrong!

hus

hus

Answer should be C.

Damien

Damien

I agree with Rooky and hus. The answer should be C in order to stop specific characters from being entered.

M

M

i think is C answer to

Bilbo

Bilbo

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.

carri

carri

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.

Shahid Mahmood Khawaja

Shahid Mahmood Khawaja

C is the Correct Answer.

C#

C#

Why no d?

denis

denis

+++

Tom

Tom

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.

denis

denis

b and d are equal

Hai

Hai

b and d. both are ok.
A & C : error when users copy and paste using mouse

anii

anii

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)

j man

j man

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

hab

hab answer is C