472,328 Members | 967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

Restrict user input

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
10 8782
<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
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
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
<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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: N?ant Humain | last post by:
I have just begun learning Python so that I can write a simple script to make modification of a file used by another Python script easier. This...
1
by: Fei Li | last post by:
Hi, Suppose my form has menu, tool bar, tool panel and drawing panel. How to restrict user mouse, say, in the area of tool bar and drawing panel?...
3
by: dei3cmix | last post by:
Hey, I am having a problem with a program I am working on. Basically, the first part of the program gets input from a file using cin.getline. Then...
2
by: danielboendergaard | last post by:
Hey Im making a homepage in php. I use a html form to put data into mysql and i want to make some buttons which inserts user input values into a...
9
by: chuck | last post by:
I need some help with validating user input. I am writing a C computer program for an intro to C course. Here is the situation. I am creating an...
5
by: SKS | last post by:
hi all i would like to know how to restrict user from entering numeric value ina textbox..using C# if user try to enter text it shouldnt be...
12
by: Tarique | last post by:
I have tried to restrict the no. of columns in a line oriented user input.Can anyone please point out potential flaws in this method? btw.. 1.I...
4
by: simon2x1 | last post by:
i have a text box i mean textarea(<textarea></textarea>) in my page i want to restrict user no to post more than 40 character how can i do that
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.