473,513 Members | 2,545 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8875
<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
3425
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 file is basically a list of regular expressions. What I want to do is allow the user to select one of these regular expressions to modify, but I've realized I know of no way to provide a default value...
1
4137
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? Thanks
3
3675
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 the second part, (still in the same main as the first part) needs to get input from the user, and I want to do this with cin.getline also. The problem I am getting, is when I run the program, the...
2
9646
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 textarea. I have used a button like this: <input type="button" value="Add Quote" onclick="document.getElementById('nyhed').value+=''"> The button works fine and insterts into the textarea....
9
5826
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 application that will do currency conversions. The user will be presented with a list of 5 selections they can make. They will then be prompted for which selection they want to enter (which can...
5
13476
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 allowed.. i tried errror provider.. but it doesnt met my requirement.. thanks in advance for ur valuable reply let me know the best to do it urs
12
2065
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 have not used dynamic memory allocation because thats not something i want to implement. 2.I suppose my input line oriented . 3.The maximum columns i need is 80 4.There is an infinite loop in the...
4
2255
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
7270
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7565
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5704
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4759
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3242
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.