Jump to content

Use onkeyup(textfield) or onclick(button) for registration form validation?


slyte33

Recommended Posts

I don't really need to post any code to get help with this, but I can explain what I am talking about.

I've heard using onkeyup could cause problems, but I'm unsure on this.

 

Basically, i have a registration form, but let's just concentrate on a simple "Username" field and a "Register" button to keep this simple.

Let's say the username must be at least 4 characters long.

 

Using onkeyup:

<input type='text' id='username' name='username' onkeyup="validate();"><span id=error1></span>

The user enters a username, beside the usernames textfield is a <span id=error1> tag. onkeyup event obviously will update the span id as the user types. If the username is less than 4 characters, it will display "Too short, 4 characters minimum".

 

Using onclick: 

<input type="submit"  onClick="validate();" value="Register">

You click register... it updates the span tag if there are any errors, unlike the onkeyup event which does it without clicking submit.

 

So my question is, which would be a better choice? Or is it whatever I'd like to use? I've just heard about onkeyup causing problems with some people.

 

All help would be much appreciated :)

 

Link to comment
Share on other sites

I would assign the event to the submit, but not onclick of the submit button. Assign it to the actual form onsubmit="..." event, otherwise the user could press enter to submit the form and it wouldn't be validated. It's then up to you if you want to add further validation to individual inputs as they enter text, but I would personally use a timer to delay the validation by about 2-3 seconds after they press a key, and reset the timer every time the event is called. You'd also want to bind it to the onblur event in this case. It just makes the process feel a lot smoother, not big red error messages popping up after you've entered the first character.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.