How do I process the input of a textbox, by keystroke or other entry method (swipe)?

Michael Sterling
Michael Sterling used Ask the Experts™
on
I have a text box on a form that, when data is entered, by keystroke, or by swiping a card through a card reader that populates the text box with the data read from the card, and I'd like to know how I would go about doing this. I'm guessing that in either case I could / would somehow use the length of the string in the text box to be a determining factor but how? There is a limit to the number of characters that will be input so I do know that before hand. Ultimately what I'm trying to achieve is that when the user is done entering data, I manipulate the string and replace the text (in the same textbox) with the new "processed" / "manipulated" string. How would I go about doin this? Code examples or links to examples would be appreciated...(in the markup, codebehind and or javascript if needed). Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
you can use onkeypress-event in javascript.
then you can fire a javascript function and do whatever you like with the input of the box.

Have a look here:
http://www.codingforums.com/archive/index.php/t-121148.html
http://forums.asp.net/t/1033798.aspx/1
http://www.cambiaresearch.com/c4/029c978b-aac5-472e-97a8-95b256f5febd/how-can-i-use-javascript-to-allow-only-numbers-to-be-entered-in-a-textbox.aspx

Hope that helps
Manoj PatilSr. Software Engineer
Commented:
Add following script to use enter key for textbox

<script type="text/javascript" language="javascript">

    function handleEnter(obj, event) {
        var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
        if (keyCode == 13) {
            document.getElementById(obj).click();
            return false;
        }
        else {
            return true;
        }
    } 
 </script>

Open in new window


To bind above script use following code in Page_Load()

protected void Page_Load(object sender, EventArgs e)
{
       TextBox1.Attributes.Add("onkeypress", "return handleEnter('" + btnSubmit.ClientID + "', event)");
}

Open in new window




TO ALLOW ONLY NUMBERS IN TEXT BOX ADD FOLLOWING SCRIPT IN HEAD TAG
<script>
 
  function validatenumber(event, obj)
  {
    var code = (event.which) ? event.which : event.keyCode;
    var character = String.fromCharCode(code);
 
    if ((code >= 48 && code <= 57))
    { // check digits
 
      // Disallow all numbers if the entry is 0
      if (obj.value =="0")
        return false;
 
      if (!isNaN(obj.value))
      {
        if (obj.value =="0.0" && code == 48)
        {
          alert("Value cannot be less than 0.01");
          return false;
        }
      }
 
      return true;
    }
    else if (code == 46)
    { // Check dot
      if (obj.value.indexOf(".") < 0)
      {
        if (obj.value.length == 0)
         obj.value ="0";
 
        return true;
      }
    }
    else if (code == 8 || code == 116)
    { // Allow backspace, F5
      return true;
    }
    else if (code >=37 && code <= 40)
    { // Allow directional arrows
      return true;
    }
 
    return false;
  }
 
  function validatefield(obj)
  { // Remove dot if last character
    if (obj.value.indexOf(".") == obj.value.length-1)
    {
      obj.value = obj.value.substring(0, obj.value.length-1)
    } // Clear text box if not a number, incase user drags/drop letter into box
    else if (isNaN(obj.value))
    {
      obj.value = "";
    }
    else if (obj.value <= 0.01)
    {
      obj.value = "";
      alert("Value cannot be less than 0.01.");
    }
  }
  </Script>

Open in new window

Manoj PatilSr. Software Engineer
Commented:
Sorry forgot to tell
how to use the above script for allowing only numbers

<asp:TextBox ID="txtBudget" runat="server" Width="124px" onkeypress="return validatenumber(event, this);" onblur="validatefield(this)"></asp:TextBox>

Open in new window

Michael SterlingWeb Applications Developer

Author

Commented:
used bits and pieces form all thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial