I have placed one textbox.... I want to put restriction on it ..
that digits and special characters should not be allowed to input in textbox...
how can i do using onkeypress event in Javascript ???
my code is ...
<script>
function event()
{
document.getElementById("TX1").addEventListener("keypress", handleKeyPress, false);
}
function handleKeyPress(e)
{
var restricted = "0123456789_#!";
var key = e.keyCode || e.which;
var i=0;
for(;i<restricted.length;i++)
{
if (restricted.charCodeAt(i) == key)
{
e.returnValue = false;
}
return true;
}
</script>
<body onLoad="event();">
<input type="text" name="T1" id="TX1" size="27" maxlength="35" >
</body>
I have placed one textbox.... I want to put restriction on it ..
that digits and special characters should not be allowed to input in textbox...
how can i do using onkeypress event in Javascript ???
my code is ...
<script>
function event()
{
document.getElementById("TX1").addEventListener("keypress", handleKeyPress, false);
}
function handleKeyPress(e)
{
var restricted = "0123456789_#!";
var key = e.keyCode || e.which;
var i=0;
for(;i<restricted.length;i++)
{
if (restricted.charCodeAt(i) == key)
{
e.returnValue = false;
}
return true;
}
</script>
<body onLoad="event();">
<input type="text" name="T1" id="TX1" size="27" maxlength="35" >
</body>
Share
edited Mar 13, 2011 at 10:09
Sarfraz
383k82 gold badges559 silver badges612 bronze badges
asked Mar 13, 2011 at 10:08
hiren gamithiren gamit
2,1636 gold badges25 silver badges23 bronze badges
2 Answers
Reset to default 5that digits and special characters should not be allowed to input in textbox...
Don't do this through keypress
filtering. It's user-hostile, confusing, messes up other control keypresses and is easy to defeat by using other methods of input, such as copy/paste, drag/drop, form fillers, spellcheckers, IMEs...
Instead, have a warning that visibly directs the user when they've put something unexpected in, eg.:
<input type="text" name="T1" id="TX1"/>
<div id="TX1_help">
Please do not use numbers or ‘!’, ‘_’ or ‘#’ characters in this field.
</div>
<script type="text/javascript">
var tx1= document.getElementById('TX1');
var tx1help= document.getElementById('TX1_help');
function checkTX1() {
var isok= /^[^0-9_#!]*$/.test(tx1.value);
tx1help.style.display= isok? 'none' : 'block';
}
checkTX1();
// Re-check the field every time a key is pressed or other change event.
// To ensure it is still checked in a timely fashion in the case of
// non-keyboard events, add a timer as well.
//
tx1.onchange=tx1.onkeyup= checkTX1;
setInterval(checkTX1, 500);
</script>
You can use addEventListener
for the event attachment if you want to, but be aware this won't work in IE<9, for which you would need the attachEvent
model. (event.returnValue
is specific to the IE attachEvent
model and will not work with addEventListener
, which uses event.preventDefault()
instead.)
Try the following:
document.getElementById('TX1').addEventListener('keypress', function(event) {
if (event.keyCode == 13) {
handleKeyPress();
}
});