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"/>
change is better than blur and the input needs indeed a name.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')
})
blurevent. But @Awais provided an even better solution, as thechangeevent will be raised, when the input looses focus, but only if the text changed.