By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,589 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

Restrict user input

P: n/a
hi all,
i have a textfield where i would like the user to input only Y or N.

can somebody tell me how can i restrict the user from entering any
other character, number or special character.
thanks.

Jan 18 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
<sc*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
hi all,
i have a textfield where i would like the user to input only Y or N.

can somebody tell me how can i restrict the user from entering any
other character, number or special character.
thanks.


Will this help? Watch for word-wrap.

<html>
<head>
<title>YN.htm</title>
<script type="text/javascript">
function YN(that) {
that.value = that.value.toUpperCase();
var rex = /^[YN]$/;
if (rex.test(that.value)) return;
alert("Entry must be Y or N only.")
that.value = "";
}
</script>
</head>
<body>
<form>
<input type="text" name="text" size="1"
maxlength="1" onchange="YN(this)">
</form>
</body>
</html>
Jan 18 '06 #2

P: n/a
Also you can check the onkeypress event for the textbox, just delete
the last charectar and pervious ones of similar type if their unicodes
arent that of Y and N (on the event,keyCode varible, assuming function
argument event)

Jan 18 '06 #3

P: n/a
sc*****@gmail.com wrote:
hi all,
i have a textfield where i would like the user to input only Y or N.

can somebody tell me how can i restrict the user from entering any
other character, number or special character.
thanks.


Why not use a checkbox or radio buttons instead?
--
Rob
Jan 18 '06 #4

P: n/a
<html>
<head>

<script type="text/javascript">
var returnvalue = false
function keypro(event)
{
//this function returns true if y and false if n
if (event.keyCode==89)
{returnvalue = true}
else if (event.keyCode==78)
{returnvalue = false}
else if (event.keyCode==8) //delete/backspace
{}
else
{alert("Please only enter Y or N in the textbox!")}
}

</script>
</head>
<body>

<form name="form1">
<input type="text" onkeydown="keypro(event)" name="tf1">
</form>

</body>
</html>
thats an example of my idea

Jan 18 '06 #5

P: n/a
mo********@gmail.com wrote:
<html>
<head>

<script type="text/javascript">
var returnvalue = false
function keypro(event)
{
//this function returns true if y and false if n
if (event.keyCode==89)
{returnvalue = true}
else if (event.keyCode==78)
{returnvalue = false}
else if (event.keyCode==8) //delete/backspace
{}
else
{alert("Please only enter Y or N in the textbox!")}
}
If you need to use the actual key pressed, then:

function keypro(event)
{
var x = event.keyCode || event.which;
if ( 89==x || 78==x || 8==x) return;
alert('Y or N please...');
}
is more concise but not recommended at all - it traps key presses like
return, shift, alt, ctrl, etc. which is pretty awful. You will end up
with a large number of ORs in there to let such represses go.

Much better to test the actual text entered:

function testInput(el)
{
var x = el.value;
if (x == '' || /^[YyNn]$/.test(x)) return;
el.value = '';
alert('Only Y or N please...');
}
and use the keyup event:

<input type="text" onkeyup="testInput(this);"
maxlength="1" name="tf1">

[...]
thats an example of my idea


Perhaps you should reply to the OP.
--
Rob
Jan 18 '06 #6

P: n/a
great replies guys. its good to see that every problem has more than
one solution and everybody is different.

just another quick one, is there a list of keycodes and can somebody
provide a link for it.

thanks to all.

Jan 18 '06 #7

P: n/a
sc*****@gmail.com wrote:
great replies guys. its good to see that every problem has more than
one solution and everybody is different.

just another quick one, is there a list of keycodes and can somebody
provide a link for it.


You can easily discover that using a simple script:

<input type="text" onkeypress="showKeyCode(event, 'xx');">
<div>keyCode: <span id="xx"></span></div>

<script type="text/javascript">
function showKeyCode(e, id)
{
var e = e || window.event;
var x = e.keyCode || e.which;
document.getElementById('xx').innerHTML = x;
}

</script>

But it was suggested that you don't use keycode because you have a
potentially unknown number of keycodes to deal with. You also want to
deal with some of them differently, so there are at least 3 classes of
code: acceptable, ignore and raise error.

You also don't know if all browsers or user agents have the same
keycodes for all their keys, e.g. Windows 'window' key, Mac OS Apple
key, special function keys, etc.

Using the entered text, you only have to deal with two cases:

1. the value is one of Y, y, N, n or empty string so do nothing,
2. anything else causes an action (error message, etc.)

--
Rob
Jan 18 '06 #8

P: n/a
Yeah I agree w/ Rob but I have found this to be useful:
http://www.w3schools.com/js/tryit.as..._event_keycode
press the key you wish to look up on it'll popup the keycode, also
shows you the source.

Jan 18 '06 #9

P: n/a
mo********@gmail.com wrote:
Yeah I agree w/ Rob but I have found this to be useful:
http://www.w3schools.com/js/tryit.as..._event_keycode
press the key you wish to look up on it'll popup the keycode, also
shows you the source.


You can use that if you like having to use your mouse to clear the alert
boxes all the time, or you can use mine - no alerts to clear, see every
key as you press it.

Oh, and the code is right here. :-)
--
Rob
Jan 20 '06 #10

P: n/a
RobG said the following on 1/20/2006 1:13 AM:
mo********@gmail.com wrote:
Yeah I agree w/ Rob but I have found this to be useful:
http://www.w3schools.com/js/tryit.as..._event_keycode
press the key you wish to look up on it'll popup the keycode, also
shows you the source.


You can use that if you like having to use your mouse to clear the alert
boxes all the time,


<spacebar> or <enter> key, no mouse needed to dismiss alerts.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 20 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.