Click here to Skip to main content
15,921,837 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to capture the keypress values of textbox under button click.
Posted
Comments
What have you tried?
Member 10380324 5-Nov-13 7:03am    
How to set the same functionality under button click

<script type=""text/javascript">

language="javascript">
function isNumberKey(evt) {
// if (!(((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)) || (event.keyCode == 8) || (event.keyCode == 9) || (event.keyCode == 37) || (event.keyCode == 39) || (event.keyCode == 46) || (event.keyCode == 190) || (event.keyCode == 35) || (event.keyCode == 36)))
//event.returnValue=false;
var charCode = (evt.which) ? evt.which : event.keyCode
document.getElementById('HDVALUE'.value)=charCode;
if (charCode > 31 && charCode < 48 || charCode > 57)
if (event.keyCode != 46)
if (event.keyCode != 110 || event.keyCode ==32)
{

alert("Special Characters or Spaces not allowed");
return false;
}

return true;



}


</script>

<asp:TextBox ID="TextBox1" runat="server" onkeypress="return isNumberKey(event)">
expert-programmer 6-Nov-13 1:03am    
Wow nice solution. I think Member 10380324 get the best solution.

1 solution

I presume you textbox has the focus in order for you to type into it (i.e., keypress).

Use the onkeyup event (I find it easier as the change has already been implemented) to use the DOM to get the value of your textbox. Your textbox will need an id='something' so that document.getElementById('something').value returns your textbox's content.

If you only wish to capture the data upon a button click, use the onclick event and do the same thing.

You can, by the way, modify the content at this time, too, such as changing case, limiting input to only certain characters, formatting (like telephone number), or pretty much anything javaScript can do.

 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900