23

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)"/>
3
  • no this is for register that if this user exist cannot submit whit this username. Commented Feb 3, 2011 at 12:31
  • What you are looking for is the blur event. But @Awais provided an even better solution, as the change event will be raised, when the input looses focus, but only if the text changed. Commented Feb 3, 2011 at 12:41
  • possible duplicate of Detect changed input text box Commented Oct 30, 2014 at 17:19

5 Answers 5

25

Call the function on the change event of text field:

<input name="username" onChange="check_user(this.value);" type="text"/>
Sign up to request clarification or add additional context in comments.

5 Comments

i did this but when switch between inputs by clicking on others or tab for switch function didnt respond!
are you calling function with correct spellings on all inputs>
+1, change is better than blur and the input needs indeed a name.
Thanks Felix Kling agree with u
@mamrezo: Double check your code, it should work fine: jsfiddle.net/VNV5P/1
15

You need to use the "onblur" event. It fires when a control loses its focus, which seems to be exactly what you need.

Comments

13

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.

Comments

1

I think you are looking for check the duplication of user. you have to write server side code for that.

1 Comment

i know this is an ajax work but instead of onkeyup what should i type?
0

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')
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.