473,320 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do I control textbox input for every keypress in ASP.NET

Are there any javascript codes there?
>

Answer: Yes

On the PageLoad event call InitialClientControsl as follows

/// <summary>
/// This will add client-side event handlers for most of the
form controls so
/// that proper behaviors will happend ( uppercase entry,
running total,etc.).
/// This will also register the call to client script to compute
the total.
/// </summary>
private void InitializeClientControlsHandlers()
{
txbName.Attributes.Add("onkeypress", "return
InputUpperCaseOnly(event)" );

txbExpiryDateMM.Attributes.Add("onkeypress", "return
InputNumberOnly(event)" );
txbExpiryDateMM.Attributes.Add("onfocusout",
"TwoDigits(this)" );
txbExpiryDateMM.Attributes.Add("onkeyup",
"JumpIfFilledUp(event, this,
document.getElementById('txbExpiryDateYY'), 2 )" );

txbExpiryDateYY.Attributes.Add("onkeypress", "return
InputNumberOnly(event)" );
txbExpiryDateYY.Attributes.Add("onfocusout",
"TwoDigits( this)" );
txbExpiryDateYY.Attributes.Add("onkeyup",
"JumpIfFilledUp( event, this, document.getElementById('txbOrder00'),
2 )" );

txbCardNumber.Attributes.Add("onkeypress", "return
InputNumberOnly(event)" );

foreach(TextBox ctrl in amountControls)
{
ctrl.Attributes.Add("onkeyup","getTotalAmount()");
ctrl.Attributes.Add("onchange","getTotalAmount()") ;
ctrl.Attributes.Add("onkeypress","return
InputNumberOnly(event)");
}

foreach(TextBox ctrl in orderControls)
{
ctrl.Attributes.Add("onkeypress","return
InputUpperCaseNoSpaceOnly(event)");
}

rbtnCredit.Attributes.Add("onclick", @"if (! confirm('You
have selected a CREDIT RETURN transaction.\r\n \r\n Press OK BUTTON
if this correct.') )
document.getElementById('rbtnAuthorize').click();" );

Page.RegisterClientScriptBlock("getTotalAmount",
CommonUtil.EnclosedJavascript(
" function getTotalAmount() "
," { "
," var t = 0.0; "
," var i = 0; "
," for (i = 0; i < " + MAX_ITEMS + " ; i++) "
," { "
," var sf = \"txbAmount\" + formatNumber(i,
\"00\");"
," var t1 = document.getElementById( sf); "
," if ( t1 != null ) "
," { var v1 = t1.value; "
," var val = parseFloat(v1);"
," if ( !isNaN( val ) ) t += val; "
," } "
," } "
," d = document.getElementById(\"total\"); "
," d.innerHTML = \"Total Amount :&nbsp;&nbsp;\" +
formatNumber(t, \"$#,##0.00\"); "
," } "
," function TwoDigits(x) "
," { if (x.value.length 0 ) {"
," var y = '0' + x.value;"
," x.value = y.substr( y.length-2) ; "
,"} }"

));

Page.RegisterStartupScript("PreComputeTotal",
"<script>getTotalAmount();" +
"document.getElementById( \"" + txbName.ClientID +
"\").focus(); " +
"document.getElementById( \"" + txbName.ClientID +
"\").select();</script>");
}

hre are some js script used
// Original JavaScript code by Duncan Crombie: dc******@chirp.com.au

// CONSTANTS
var separator = ","; // use comma as 000's separator
var decpoint = "."; // use period as decimal point
var percent = "%";
var currency = "$"; // use dollar sign for currency

function formatNumber(number, format, print) { // use:
formatNumber(number, "format")
if (print) document.write("formatNumber(" + number + ", \"" +
format + "\")<br>");

if (number - 0 != number) return null; // if number is NaN
return null
var useSeparator = format.indexOf(separator) != -1; // use
separators in number
var usePercent = format.indexOf(percent) != -1; // convert
output to percentage
var useCurrency = format.indexOf(currency) != -1; // use
currency format
var isNegative = (number < 0);
number = Math.abs (number);
if (usePercent) number *= 100;
format = strip(format, separator + percent + currency); //
remove key characters
number = "" + number; // convert number input to string

// split input value into LHS and RHS using decpoint as divider
var dec = number.indexOf(decpoint) != -1;
var nleftEnd = (dec) ? number.substring(0, number.indexOf(".")) :
number;
var nrightEnd = (dec) ? number.substring(number.indexOf(".") +
1) : "";

// split format string into LHS and RHS using decpoint as
divider
dec = format.indexOf(decpoint) != -1;
var sleftEnd = (dec) ? format.substring(0, format.indexOf(".")) :
format;
var srightEnd = (dec) ? format.substring(format.indexOf(".") +
1) : "";

// adjust decimal places by cropping or adding zeros to LHS of
number
if (srightEnd.length < nrightEnd.length) {
var nextChar = nrightEnd.charAt(srightEnd.length) - 0;
nrightEnd = nrightEnd.substring(0, srightEnd.length);
if (nextChar >= 5) nrightEnd = "" + ((nrightEnd - 0) + 1); //
round up

// patch provided by Patti Marcoux 1999/08/06
while (srightEnd.length nrightEnd.length) {
nrightEnd = "0" + nrightEnd;
}

if (srightEnd.length < nrightEnd.length) {
nrightEnd = nrightEnd.substring(1);
nleftEnd = (nleftEnd - 0) + 1;
}
} else {
for (var i=nrightEnd.length; srightEnd.length >
nrightEnd.length; i++) {
if (srightEnd.charAt(i) == "0") nrightEnd += "0"; // append
zero to RHS of number
else break;
}
}

// adjust leading zeros
sleftEnd = strip(sleftEnd, "#"); // remove hashes from LHS of
format
while (sleftEnd.length nleftEnd.length) {
nleftEnd = "0" + nleftEnd; // prepend zero to LHS of number
}

if (useSeparator) nleftEnd = separate(nleftEnd, separator); //
add separator
var output = nleftEnd + ((nrightEnd != "") ? "." + nrightEnd :
""); // combine parts
output = ((useCurrency) ? currency : "") + output +
((usePercent) ? percent : "");
if (isNegative) {
// patch suggested by Tom Denn 25/4/2001
output = (useCurrency) ? "(" + output + ")" : "-" + output;
}
return output;
}

function strip(input, chars) { // strip all characters in 'chars'
from input
var output = ""; // initialise output string
for (var i=0; i < input.length; i++)
if (chars.indexOf(input.charAt(i)) == -1)
output += input.charAt(i);
return output;
}

function separate(input, separator) { // format input using
'separator' to mark 000's
input = "" + input;
var output = ""; // initialise output string
for (var i=0; i < input.length; i++) {
if (i != 0 && (input.length - i) % 3 == 0) output += separator;
output += input.charAt(i);
}
return output;
}

// input filtering function
// copyright 1999 Idocs, Inc. http://www.idocs.com
// Distribute this script freely but keep this notice in place
function letternumber(e)
{
var key;
var keychar;

if (window.event)
key = window.event.keyCode;
else if (e)
key = e.which;
else
return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();

// control keys
if ((key==null) || (key==0) || (key==8) ||
(key==9) || (key==13) || (key==27) )
return true;

// alphas and numbers
else if ((("abcdefghijklmnopqrstuvwxyz0123456789").indexOf (keychar)
-1))
return true;
else
return false;
}

function InputNumberOnly(e)
{
var key;
var keychar;

if (window.event)
key = window.event.keyCode;
else if (e)
key = e.which;
else
return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();

// control keys
if ((key==null) || (key==0) || (key==8) ||
(key==9) || (key==13) || (key==27) )
return true;

// alphas and numbers
else if ((("0123456789.").indexOf(keychar) -1))
return true;
else
return false;
}
function InputUpperCaseOnly(e)
{
var key;
var keychar;

if (window.event)
key = window.event.keyCode;
else if (e)
key = e.which;
else
return true;

keychar = String.fromCharCode(key);
keychar = keychar.toUpperCase();

// control keys
if ((key==null) || (key==0) || (key==8) ||
(key==9) || (key==13) || (key==27) )
return true;

if (window.event)
window.event.keyCode = keychar.charCodeAt(0);
else if (e)
e.which = keychar.charCodeAt(0);

return true;
}

function InputUpperCaseNoSpaceOnly(e)
{
var key;
var keychar;

if (window.event)
key = window.event.keyCode;
else if (e)
key = e.which;
else
return true;

keychar = String.fromCharCode(key);
keychar = keychar.toUpperCase();

// control keys
if ((key==null) || (key==0) || (key==8) ||
(key==9) || (key==13) || (key==27) )
return true;
if (((" ").indexOf(keychar) -1))
return false;
if (window.event)
window.event.keyCode = keychar.charCodeAt(0);
else if (e)
e.which = keychar.charCodeAt(0);

return true;
}

function JumpIfFilledUp(e, obj1, obj2, len)
{
var key;
var keychar;

if (window.event)
key = window.event.keyCode;
else if (e)
key = e.which;
else
return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();

// control keys
if ((key==null) || (key==0) || (key==8) ||
(key==9) || (key==13) || (key==27) )
{
return true;
}

// alphas and numbers
// else if ((("0123456789.").indexOf(keychar) -1))
//{
else if ( obj1.value.length == len )
{
obj2.focus();
obj2.select();

}

return true;
//}
//else
// return false;


}

Feb 13 '07 #1
0 11924

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: RTM | last post by:
Can anyone help me with the following issue? I've seen some similar questions here, but none relating to a textbox control.... I have a form with several controls, one of them being a textbox...
2
by: John Lau | last post by:
Hi, Is there documentation that talks about the page lifecycle, the lifecycle of controls on the page, and the rendering of inline code, in a single document? Thanks, John
6
by: Tim Westmoreland | last post by:
Can someone tell me how to raise an event or trap when the maxlength of a server side textbox has been reached?
4
by: Tom | last post by:
I have a VB.NET user control that I wrote - this control has three or four other controls on it (textbox, combobox, datetime picker, etc). Now, whenever the control is on a form and the user enters...
2
by: bretth | last post by:
In a VB.Net Windows Forms application, I have a user control that handles mouse events. Another section of code programmatically adds a label to the control. I would like label to ignore all...
3
by: windy | last post by:
I got a question about keypress event on textbox: I found that the keypress event doesn't invoked when i press the "." button on alphabetic keyboard, however if i use numberpad's "." button...
3
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
I have VS 2005 (C#) There is a control numericUpDown so you can spin numeric values. What I need to do is to spin date (+- one day). How to do that? Moreover, I want a user to type the date as...
3
by: =?Utf-8?B?QmVybmFyZG8gU2FsYXphciBuZXdi?= | last post by:
Hi everybody... i need help with this issue: i have a textbox control with the focus. at side, i have a datagridview control populated with some data. i need to control behavior of dgv control...
2
by: Jason Huang | last post by:
Hi, How do I override a TextBox's KeyPress evnt? And how do we use it? Thanks for help. Jason
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.