Restrict user input

S

sconeek

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.
 
M

McKirahan

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>
 
M

mouseit101

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)
 
R

RobG

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?
 
M

mouseit101

<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
 
R

RobG

<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.
 
S

sconeek

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.
 
R

RobG

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.)
 
R

Randy Webb

RobG said the following on 1/20/2006 1:13 AM:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top