Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4005362
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:23:04+00:00 2026-05-20T08:23:04+00:00

I have a form field for entering a user id. The user id is

  • 0

I have a form field for entering a user id. The user id is always 6 characters so the field is limited to a maxlength of 6 characters.

The field has an onkeyup() event to call a function that looks up the user id and fills in several other form fields if the user id is valid. Most people I know have used onblur() for something like this but I never liked how a user has to tab to or click on another field before the autofilling AJAX goes off.

The function right now will return w/o doing anything if the field length is < 6 characters or if the key that was pressed is a left or right cursor field.

There’s one flaw left I haven’t been able to think around. Since the field is limited to 6 characters, if 6 characters are already entered and another key is pressed, the value of the field will not change but the function will fire and validate the field (un-necessary validation/db-query).

Is there anyway to prevent the function from going off in this case? I’m thinking it’s not possible but wanted to check. I had a thought if the field length was 6 I could check the last key pressed against the 6th character of the field, but if someone typed something like ‘a’ as the 6th character and then ‘a’ again, it wouldn’t work. I guess that could eliminate all possibilities except for the one case though one case (not perfect but better).

(rails) <%= f.text_field :uid, :size => 10, :maxlength => 6, :class => 'validate_text', :onkeyup => uid_lookup (event)', :autocomplete => :off   %>

<script type="text/javascript">
    String.prototype.trim = function() {
      return this.replace(/^\s+|\s+$/g,"");
    }
</script>
<script type="text/javascript">
        uid_lookup = function(e){
        var unicode=e.keyCode? e.keyCode : e.charCode;
        if (unicode == 37 || unicode == 39) {    // ignore a left or right arrow press
            return
        }

        var uid= $('uid').value;
        uid = uid.trim();
        $('uid).value = uid;   //uid's have no spaces, go ahead and remove if typed
        if (uid.length != 6) {
            return
        }
        // db lookup & form autofill

        }
</script>       
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:23:04+00:00Added an answer on May 20, 2026 at 8:23 am

    The solution is not to use onkeyup. The correct event to use is HTML5’s oninput, which is supported by almost every major browser out there. The one browser lacking support is—of course—Internet Explorer 8, but we can emulate the event using IE’s proprietary onpropertychange event which will fire whenever an input element’s value property changes.

    I’m not familiar with rails, but the best way to apply the event is using JavaScript so that you can gracefully degrade if oninput isn’t supported:

    var el = document.getElementById("myEl");
    
    // Check support
    if ("onpropertychange" in el && !("oninput" in el)) {
        el.onpropertychange = function () {
            if (event.propertyName == "value")
                uuid_lookup.call(this, event);
        }
    }
    else
        el.oninput = uuid_lookup;
    

    The other great thing about this event is that it only fires when the value changes – much like onchange but more real-time. This means you can do away with your key detection for left and right arrows in the uuid_lookup function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a field in a CCK node form that is hidden, and has
I have a field in my form labeled Name that will contain both the
I have a form that has multiple fields, and for testing purposes is there
I have a form field (a series of checkboxes) that's being created dynamically from
I am entering data in the text field of my form that contains 2
I have a rails form with a datetime_select field. When I try to submit
Every time I have to build a form with a DateTime field I try
I have a form with multiple fields that I'm validating (some with methods added
I have asp.net form that contains fields. When I access this window, my javascript
Say you have a web form with some fields that you want to validate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.