When a user creates a new account on my website I want to check if the name already exists. I want this check to run when they leave the username input box and go to enter a password.
I tried :
<开发者_如何学运维input type="text" onkeyup="check_user(this.value)"/>
Call the function on the change
event of text field:
<input name="username" onChange="check_user(this.value);" type="text"/>
You need to use the "onblur" event. It fires when a control loses its focus, which seems to be exactly what you need.
Because you used onkeyup
in your code example, it sounds like you are looking to call a function on every keystroke. If that is the case, then instead of onchange
- which fires when the input
loses focus - you should use oninput
which will fire anytime there is a change inside of input.
I think you are looking for check the duplication of user. you have to write server side code for that.
If you want to fire when something specific is type into the window:
phr = 'lol'
ltr = phr.split('')
text = ''
document.addEventListener('keyup',function(k){
if (ltr.includes(k.key)) {
text += k.key
if (text == phr) {
console.log('Typed!')
text = ''
}
} else {
text = ''
}})
As a function your can use it like:
function wtt(phrase,clb) {
phr = phrase
ltr = phr.split('')
text = ''
document.addEventListener('keyup',function(k){
if (ltr.includes(k.key)) {
text += k.key
if (text == phr) {
clb()
text = ''
}
} else {
text = ''
}})
}
wtt('my phrase', function () {
console.log('dostuff')
})
精彩评论