472,951 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Check numbers entered

I am constructing an HTML questionnaire and one of the questions
requires people to rate some choices from 1 to 5, where 1 is their
favourite and 5 is their least favourite:

Car
Bus
Taxi cab
Train
Airplane

Each choice has an INPUT TYPE=TEXT tag to contain the response. I
also have a function that is called ONCHANGE in order to check that a
number is entered between 1 and 5:

function CheckNos(obj) {
if(obj.value.match(/[^\d]/) || obj.value<1 || obj.value>5)
{alert("Please enter a number between 1 and 5");obj.value="";return
false}
}

This works fine, but ideally I would like a function that could check
ONCHANGE that someone has not filled in the same number twice, for
example answered "1" for all of them. They are only allowed to use
each rating number once, so rating their choice 1 to 5.

Can anyone suggest a short function that could accomplish this easily?
Could I use some kind of array with 5 elements and allocate a flag
value once that number had been chosen, then check that the value had
been set?

I don't have a large amount of experience in Javascript so perhaps
someone could help me.

Thanks

Steve Wylie
United Kingdom
Jul 23 '05 #1
5 6257
Lee
Steve Wylie said:

I am constructing an HTML questionnaire and one of the questions
requires people to rate some choices from 1 to 5, where 1 is their
favourite and 5 is their least favourite:

Car
Bus
Taxi cab
Train
Airplane

Each choice has an INPUT TYPE=TEXT tag to contain the response. I
also have a function that is called ONCHANGE in order to check that a
number is entered between 1 and 5:

function CheckNos(obj) {
if(obj.value.match(/[^\d]/) || obj.value<1 || obj.value>5)
{alert("Please enter a number between 1 and 5");obj.value="";return
false}
}

This works fine, but ideally I would like a function that could check
ONCHANGE that someone has not filled in the same number twice, for
example answered "1" for all of them. They are only allowed to use
each rating number once, so rating their choice 1 to 5.

Can anyone suggest a short function that could accomplish this easily?
Could I use some kind of array with 5 elements and allocate a flag
value once that number had been chosen, then check that the value had
been set?


I'll assume that this is not a class assignment.

Since the elements of the form are accessible as an array, you don't
need to create a new one. If each entry is checked as it is changed,
the only possible source of duplication is for this new value to be
a duplicate of one existing value. All you need to do is to check
each other member of this ranking group against this new value.

Here's a simple (not completely idiot-proof or the most efficient)
solution that's flexible enough to use more than once in the same
form with a different number of members in each ranking group:

<html>
<head>
<script type="text/javascript">
function checkRank(box,firstName,count){

// *box* is the text field whose onChange handler called us.
// *firstName* is the name of the first field in this rank group.
// *count* is the number of items being ranked.
// Note that *count* must be less than 10.
//
// Check that text field *box* has a value from 1 to *count*,
// then check *count* text fields beginning at *firstName*,
// ensuring that no other field has the same value as *box*.
var errMsg="Please enter a digit 1-"+count+"\n"
+"that has not been used previously."

box.value=box.value.replace(/\s*/g,""); // remove spaces

if(!box.value.search(/^$/)) return; // this change is a deletion

if(box.value.search(new RegExp("^[1-"+count+"]$"))){
alert(errMsg);
box.value="";
return;
}

var group=box.form.elements;

for(var i=0;i<group.length;i++){
if(group[i].name==firstName){
firstIndex=i;
break;
}
}
var firstIndex=i; // will be group.length if no match

if(firstIndex+count-1>group.length){
alert("Bad arguments! Fire the web designer!");
return;
}

for(var i=0;i<count;i++){
if(group[firstIndex+i]!=box &&
group[firstIndex+i].value==box.value){
alert(errMsg);
box.value="";
return;
}
}

}
</script>
</head>
<body>
<form>

<table>
<tr>
<td align="right">Car</td>
<td><input name="car" size="3" onchange="checkRank(this,'car',5)"></td>
</tr>
<tr>
<td align="right">Bus</td>
<td><input name="bus" size="3" onchange="checkRank(this,'car',5)"></td>
</tr>
<tr>
<td align="right">Taxi cab</td>
<td><input name="taxi" size="3" onchange="checkRank(this,'car',5)"></td>
</tr>
<tr>
<td align="right">Train</td>
<td><input name="train" size="3" onchange="checkRank(this,'car',5)"></td>
</tr>
<tr>
<td align="right">Airplane</td>
<td><input name="plane" size="3" onchange="checkRank(this,'car',5)"></td>
</tr>
</table>
<input type="reset" value="clear">
</form>
</body>
</html>

<html>
<head>
<script type="text/javascript">
function checkRank(box,firstName,count){

// *box* is the text field whose onChange handler called us.
// *firstName* is the name of the first field in this rank group.
// *count* is the number of items being ranked.
// Note that *count* must be less than 10.
//
// Check that text field *box* has a value from 1 to *count*,
// then check *count* text fields beginning at *firstName*,
// ensuring that none has the same value as *box*.

var errMsg="Please enter a digit 1-"+count+"\n"
+"that has not been used previously."

box.value=box.value.replace(/\s*/g,""); // remove spaces

if(!box.value.search(/^$/)) return; // change is a deletion

if(box.value.search(new RegExp("^[1-"+count+"]$"))){
alert(errMsg);
box.value="";
return;
}

group=box.form.elements;

for(var i=0;i<group.length;i++){
if(group[i].name==firstName){
firstIndex=i;
break;
}
}
var firstIndex=i; // will be group.length if no match

if(firstIndex+count-1>group.length){
alert("Bad arguments! Fire the web designer!");
return;
}

for(var i=0;i<count;i++){
if(group[firstIndex+i]!=box &&
group[firstIndex+i].value==box.value){
alert(errMsg);
box.value="";
return;
}
}

}
</script>
</head>
<body>
<form>

<table>
<tr>
<td align="right">Car</td>
<td><input name="car" size="3" onchange="checkRank(this,'car',5)"></td>
</tr>
<tr>
<td align="right">Bus</td>
<td><input name="bus" size="3" onchange="checkRank(this,'car',5)"></td>
</tr>
<tr>
<td align="right">Taxi cab</td>
<td><input name="taxi" size="3" onchange="checkRank(this,'car',5)"></td>
</tr>
<tr>
<td align="right">Train</td>
<td><input name="train" size="3" onchange="checkRank(this,'car',5)"></td>
</tr>
<tr>
<td align="right">Airplane</td>
<td><input name="plane" size="3" onchange="checkRank(this,'car',5)"></td>
</tr>
</table>
<input type="reset" value="clear">
</form>
</body>
</html>

Jul 23 '05 #2
Hi Lee

Thanks for replying with your suggested script. It actually kills 3 birds
with one stone as I notice it also checks that the entry is a valid number
and also that it is within the specified range, so that eliminates the need
for me to use my existing function just to do that on its own.

I'll have to wait till I get back to work on Thursday to try it out on my
actual questionnaire.

No, it's not a school assignment(!) I work for a local government authority
in south-east England and your script will be used in the client-side
validation of one of our consultation questionnaires we put out, asking
residents in our district their views on crime, licensing laws and fire
safety. So all pretty worthwhile stuff I think you'll agree. Think I'll
put a comment line at the top of your function, "This script by Lee", not
that anyone will see it unless they look at the source!

Thanks again for your assistance.

Steve Wylie
Jul 23 '05 #3
JRS: In article <f2**************************@posting.google.com >, seen
in news:comp.lang.javascript, Steve Wylie <st*****@hotmail.com> posted
at Tue, 22 Jun 2004 05:17:45 :
I am constructing an HTML questionnaire and one of the questions
requires people to rate some choices from 1 to 5, where 1 is their
favourite and 5 is their least favourite: Each choice has an INPUT TYPE=TEXT tag to contain the response. I
also have a function that is called ONCHANGE in order to check that a
number is entered between 1 and 5:
function CheckNos(obj) {
if(obj.value.match(/[^\d]/) || obj.value<1 || obj.value>5)
{alert("Please enter a number between 1 and 5");obj.value="";return
false}
}
Between 1 & 5, the only integers are the digits 2 3 4.

function CheckNos(obj) {
if (!obj.value.match(/^[1-5]$/)) {
alert("Please enter a digit in 1 to 5") ;
obj.value="" ;
return false }
return true }

function CheckNos(obj) {
var OK = (obj.value.match(/^[1-5]$/))
if (!OK) {alert("Please enter a digit in 1 to 5") ; obj.value="" }
return OK }
This works fine, but ideally I would like a function that could check
ONCHANGE that someone has not filled in the same number twice, for
example answered "1" for all of them. They are only allowed to use
each rating number once, so rating their choice 1 to 5.

Can anyone suggest a short function that could accomplish this easily?
Could I use some kind of array with 5 elements and allocate a flag
value once that number had been chosen, then check that the value had
been set?


Better to check on finished, since it is legitimate to put 1 2 3 1 5
then change the first one to a 4.

If you check the product for being 120, I think only 2 2 2 3 5 permuted
will falsely pass. If you check also that the sum is 15 ... .

Number them 1 2 3 5 7, check with /^[12357]$/, and you need a product of
210, which no other combination of 1 2 3 5 7 can give.

The following should work, more generally :
var A = [] // an empty array
for each entry, get value V, then A[V] = 1
check that A[1]..A[5] all exist.
or
var A = []
for each entry, get value V, then A[V] = '#'
OK = A.join('').length==5

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #4
Thank you for your response, Dr John, but I think I'll stick with
Lee's suggestion because I can't understand your number play!
(Grade 4 CSE Maths, mate - this isn't my forté!)

Thanks anyway.

Steve
Jul 23 '05 #5
JRS: In article <f2**************************@posting.google.com >, seen
in news:comp.lang.javascript, Steve Wylie <st*****@hotmail.com> posted
at Thu, 24 Jun 2004 02:45:10 :
Thank you for your response, Dr John, but I think I'll stick with
Lee's suggestion because I can't understand your number play!
(Grade 4 CSE Maths, mate - this isn't my forté!)


The paragraphs containing the numbers 120 & 210 only require what in my
time was taught to eight-year-olds; I don't know what CSE requirements
are.

For a set of numbers in 1..5 to multiply to 120 they must contain one 5,
and the others must make 24. To make 24, there must be a 3, and the
others must make 8. 8 can only be made as 4*2*1 and 2*2*2.

To get 210 by multiplying five numbers selected from 1 2 3 5 & 7, there
must be a 7, a 5, a 3, a 2 and a 1 (7 5 3 2 have no common factors).

The following paragraphs of that article show what should be the
shortest and most efficient check for the general case; but of course
they are a component rather than a fully-built answer.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Earl | last post by:
I'm curious if there are others who have a better method of accepting/parsing phone numbers. I've used a couple of different techniques that are functional but I can't really say that I'm totally...
2
by: savvy | last post by:
I'm developing a jobsite in which i'm working on Job Posting section. When the job is posted by the employer or any other agency they need to fill in all the related details in the respective...
14
by: Ørjan Langbakk | last post by:
I have a form where the user has the possibility to enclose his name. email, address and phonenumber. I want to be able to check if some of the fields are filled - at least one. This is so that...
4
by: John Salerno | last post by:
My code is below. The main focus would be on the OnStart method. I want to make sure that a positive integer is entered in the input box. At first I tried an if/else clause, then switched to...
19
Frinavale
by: Frinavale | last post by:
Filtering user input is extremely important for web programming. If input is left unfiltered users can input malicious code that can cripple your website. This article will explain how to make...
9
by: eggie5 | last post by:
How would I check if a string is a number? e.g: 'asdf2' =false '34' =true 'asf' =false '0' =true
3
by: powerej | last post by:
writing a program that asks how many numbers the users want to enter, then input an integer for the amount of numbers said, output: number of integers entered, sum of them, average of them, maximum...
39
by: emre esirik(hacettepe computer science and enginee | last post by:
int n_mines; printf("How many mines do you want in the minefield?"); scanf("%d", &n_mines); while(n_mines>100 || n_mines<0) { printf("Please only enter between 1 and 100"); scanf("%d",...
4
by: beginers of c | last post by:
hai friends see i have to get numbers in this manner 3 4 5 6 7 8 9 ok.And also my numbers should not be larger than 9.so for each number the condition has to be checked if it's great than 9 ,...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when 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.