Recent Post

Thursday, October 10, 2013

Định dạng chỉ cho nhập số trong textbox của asp.net bằng javascript

by Unknown  |  at  5:46 PM



B1: tạo javascript để dịnh dạng key tên :dinhdangchinhapso.js



function ValidateKeypress(numcheck, e) {

var keynum, keychar, numcheck;

if (window.event) {//IE

keynum = e.keyCode;

}

else if (e.which) {// Netscape/Firefox/Opera

keynum = e.which;

}

if (keynum == 8 || keynum == 127 || keynum == null || keynum == 9 || keynum == 0 || keynum == 13) return true;

keychar = String.fromCharCode(keynum);

var result = numcheck.test(keychar);

return result;

}






B2: gọi javacript trên vào web



<script type="text/javascript" src="dinhdangchinhapso.js"></script>

thêm sự kiện onkeypress cho textbox cần dịnh dạng

<asp:TextBox ID="txt1" onkeypress="return ValidateKeypress(/\d/,event);" runat="server" />

0 comments:

Proudly Powered by Blogger.