1

I have a form saying create account and I have email Id and password fields, I want to make sure that the username is unique

is there anyway that I can do this check using ajax as soon as user completes entering the username i.e. after he clicks on the password field (or he hits tab key after entering the username)

Thanks Any efforts will be appreciated

koool
  • 15,157
  • 11
  • 45
  • 60

4 Answers4

2

use "onchange" on the username field, then use a standard AJAX query.

What is the difference between onBlur and onChange attribute in HTML?

Community
  • 1
  • 1
jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107
  • I think onchange will be activated everytime a user enters a character in the username field? Is that correct? – koool Jun 04 '11 at 23:11
2

You can use a (jQuery) .blur() or (standard JS) .onBlur() event on the username field to trigger an AJAX call so you can immediately check whether or not the username is in the database when the user leaves the textfield.

Joris Ooms
  • 11,880
  • 17
  • 67
  • 124
  • does the .blur or .onBlur activates only after the user leaves the username field – koool Jun 04 '11 at 23:10
  • Yeah. Blur is the opposite of Focus: when a user clicks a field, it gets 'focus', when the user leaves a field, it loses focus (thus 'blurs') and this triggers the blur event. – Joris Ooms Jun 04 '11 at 23:12
  • 1
    onblur will be called even if the field is unchanged. not a big deal, but onchange will eliminate unnecessary checks – jcomeau_ictx Jun 04 '11 at 23:12
  • @jcomeau_ictx as i asked under your answer does onchange is called after every keystroke – koool Jun 04 '11 at 23:15
2

The only thing about the onchange is when does it fire and how does that work across the browsers. I would probably go with onblur to make sure that the user is actually done with field. If the onchange happens each time the keypress event is done, you could end up with a lot of extraneous ajax calls that could bog down the system. Again, onblur would be my choice.

hivie7510
  • 1,246
  • 10
  • 23
  • 1
    koool, I would still recommend that when they press whatever submit button that you have that you validate one final time. – hivie7510 Jun 04 '11 at 23:17
  • @hivie7510 I appreciate you advise could you please explain why doing so is beneficial?? – koool Jun 04 '11 at 23:20
  • 1
    You know, javascript is a funny thing and funny things do not always do what we want them to do. In the same way that you would do validation in the user interface and then again in business logic, you may want to do that here. The way that I look at things like security and data protection is that the interface is a suggestion of security, but the business logic (data layer, database, etc..) is the rule. Since I work typically in distributed systems, I have to do validation at each level because there is no true guarantee that the level below me has done it or done it correctly. – hivie7510 Jun 04 '11 at 23:25
  • okay so you say that in distributed systems it is better to perform validation after the final submit right? – koool Jun 04 '11 at 23:30
  • 1
    What I was really trying to say is that at each level you need to validate. So I was trying to apply that same concept here. Since the submit button that the last exit point before the data is sent on the Ajax call, then it would be good to perform one final validation. In distributed, you do not always know who is sending the data downstream, so you have to take the responsibility to make sure the data maintains its integrity. – hivie7510 Jun 04 '11 at 23:32
0

You could:

  1. bind a focus event to the username field
  2. then in this function check if the username has been input
  3. if it is use the AJAX method to send the username across to the server to check
  4. receive a response from the server side for the AJAX request and respond appropriately on the front end
8bitme
  • 897
  • 2
  • 16
  • 24