Thursday, September 20, 2007

Restrict Alphabet Input In TextBox Using Regular Expression

<html>
<head>
<script language="javascript">
function blockChar()
{
var str = document.getElementById('txt').value;
str = str.replace(/[^\d]*/g,'');
document.getElementById('txt').value = str;
}
</script>
</head>
<body onload="javascript:document.getElementById('txt').focus();">
<input type="text" id='txt' onkeyup="blockChar();" />
</body>
</html>


Program Demo

Note:IE Supported

2 comments:

Pushkar Firodiya said...

I want allow "12.39" i.e i want to allow decimal point how do i do that?

windows_mss said...

hi Pushkar Firodiya ,

if u want to decimal point meanse replace the current Reg Exp with the folloing one
str.replace(/[^\d.]*/g,'');