473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Text form validation help needed

Dear Group,

I have a text field that I wish to cellect numbers of bird seen. However,
some folk want to use 'c' before the number or '+' after to indicate a fuzzy
number. I have tried creating a function that checks for each of these and
still validates the rest as a number:

function checkNumber( an_item )
{
count_val = an_item.value;
len = count_val.length;
count = "";
if (count_val.charAt(0) == "c"){
count = count_val.substring(1,len);
} else if (count_val.charAt(len-1) == "+"){
count = count_val.substring(0,len-1);
} else {
count = count_val;
}
if (isNaN (count)) {
msg = "Error: " + count + " is not a valid number for the Count
field";
alert (msg);
an_item.value = "";
return false;
} else {
count_num = parseInt (count);
if ((count_num < 1) || (count_num > 999999)) {
msg = "Error: The number in the Count field must be\nbetween 1 and
999,999";
alert (msg);
an_item.value = "";
return false;
}
}
}
This seems to work if I enter c1000 and 1000+ birds (both accepted), and
also if I enter ca1000 it catches that. But, it seems to accept +1000 and I
can't explain why. Any takers for an improvement? For info, I'll be handling
the 'c' and '+' later, but I just need to make sure it is only these options
(other than a pure number) that come through.

Thanks
Iain
--

__________________________________________________ _________
Iain Downie
British Trust for Ornithology, The Nunnery,
Thetford, Norfolk IP24 2PU, UK ® Charity No. 216652
Tel: +44 (0)1842 750050, fax: +44 (0)1842 750030
BirdWeb Gateway: http://www.bto.org/birdweb
Jul 23 '05 #1
5 1264
Ivo
"Iain Downie" wrote
I have a text field that I wish to cellect numbers of bird seen. However,
some folk want to use 'c' before the number or '+' after to indicate a
fuzzy number. I have tried creating a function that checks for each of
these and still validates the rest as a number:

function checkNumber( an_item )
{
count_val = an_item.value;
len = count_val.length;
count = "";
if (count_val.charAt(0) == "c"){
<snip code>

This seems to work if I enter c1000 and 1000+ birds (both accepted), and
also if I enter ca1000 it catches that. But, it seems to accept +1000 and
I can't explain why. Any takers for an improvement? For info, I'll be
handling the 'c' and '+' later, but I just need to make sure it is only
these options (other than a pure number) that come through.


A regex might be an idea. In the following example, spaces between the
number and either the c or the + are allowed, so "c 99" will validate. If
you don't want this, remove the two occurances of " \s* " in the code below.

function checkNumber( an_item ) {
count_val = an_item.value;
x = count_val.match(/(^c?\s*(\d+)$)|(^(\d+)\s*\+?$)/);
if(!x)
{ alert('Please keep to the right format.'); return false;}
else if(x[2]<1||x[2]>999999)
{ alert('Please enter a number below 999999.'); return false;}
return true;
}
HTH
Ivo


Jul 23 '05 #2
Iain Downie wrote:
Dear Group,

I have a text field that I wish to cellect numbers of bird seen. However,
some folk want to use 'c' before the number or '+' after to indicate a fuzzy
number. I have tried creating a function that checks for each of these and
still validates the rest as a number:

function checkNumber( an_item ) function checkNumber( an_item ){
// You can get count from "an_item" and check that its between 1 & 999999

count=Number("an_item.value".replace(/[^\d]/g,""));
if(!count){alert("No number entered"); return false;}
if(count>999999) {alert("Too many"); return false;}
// This doesn't check for "c" or "+" though, a simple
// regex will do that, but will return true
// if a number (1-999999)is entered.
// You may want to store "count" in hidden field:

an_item.form.hiddenFieldName.value=count;
return true;
}

Mick

{
count_val = an_item.value;
len = count_val.length;
count = "";
if (count_val.charAt(0) == "c"){
count = count_val.substring(1,len);
} else if (count_val.charAt(len-1) == "+"){
count = count_val.substring(0,len-1);
} else {
count = count_val;
}
if (isNaN (count)) {
msg = "Error: " + count + " is not a valid number for the Count
field";
alert (msg);
an_item.value = "";
return false;
} else {
count_num = parseInt (count);
if ((count_num < 1) || (count_num > 999999)) {
msg = "Error: The number in the Count field must be\nbetween 1 and
999,999";
alert (msg);
an_item.value = "";
return false;
}
}
}
This seems to work if I enter c1000 and 1000+ birds (both accepted), and
also if I enter ca1000 it catches that. But, it seems to accept +1000 and I
can't explain why. Any takers for an improvement? For info, I'll be handling
the 'c' and '+' later, but I just need to make sure it is only these options
(other than a pure number) that come through.

Thanks
Iain
--

__________________________________________________ _________
Iain Downie
British Trust for Ornithology, The Nunnery,
Thetford, Norfolk IP24 2PU, UK ® Charity No. 216652
Tel: +44 (0)1842 750050, fax: +44 (0)1842 750030
BirdWeb Gateway: http://www.bto.org/birdweb

Jul 23 '05 #3
JRS: In article <cd*******************@news.demon.co.uk>, seen in
news:comp.lang.javascript, Iain Downie <ia*********@bto.org> posted at
Thu, 15 Jul 2004 17:24:54 :
I have a text field that I wish to cellect numbers of bird seen. However,
some folk want to use 'c' before the number or '+' after to indicate a fuzzy
number. I have tried creating a function that checks for each of these and
still validates the rest as a number: count_num = parseInt (count);


Read the FAQ. That code will silently give the wrong number if someone
enters, say, 077. Generally, parseInt is used far more often than is
necessary; and often wrongly.

Signatures should not exceed four lines after a separator line minus
minus space. No-one reasonable would complain about your five text
lines, but the three empty lines and the rule are unnecessary.

AFAICS, it is *always* best to validate input with a RegExp - see
<URL:http://www.merlyn.demon.co.uk/js-valid.htm>. In this case, you can
use ([1-9]\d{0,5}) as part of the RegExp - that means all-digit,
compulsory first digit 1-9, digit count 1-6; so there is no need to test
arithmetically.

OK = /^(c?)([1-9]\d{0,5})(\+?)$/.test(count_val)
if (!OK) { alert{"Blunder") ; ... }
count_num = +RegExp.$2 // number
Roughly = !!( RegExp.$1 + RegExp.$3 ) // boolean

That allows c before and/or + after. The !! is not-not, and forces
Boolean conversion; IIRC, an empty string -> false and a non-empty ->
true.

If you have a thousands separator in a message, you should allow one in
the data; best omit.

Would "?" be better as a fuzzy indicator? Note that 'c' means 'circa'
means "about" and '+' means 'over'; they can be interpreted differently
by testing $1 & $3 separately.

--
© 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
Ivo
A regex might be an idea. In the following example, spaces between the
number and either the c or the + are allowed, so "c 99" will validate. If
you don't want this, remove the two occurances of " \s* " in the code below.
function checkNumber( an_item ) {
count_val = an_item.value;
x = count_val.match(/(^c?\s*(\d+)$)|(^(\d+)\s*\+?$)/);
if(!x)
{ alert('Please keep to the right format.'); return false;}
else if(x[2]<1||x[2]>999999)
{ alert('Please enter a number below 999999.'); return false;}
return true;
}


Unfortunately this code pulls up the alert 'number below 999999' error if I
enter 1000+. I need both c1000 and 1000+ to be valid, or just 1000.

I have a problem with regex code snippets in general - I have no idea how
they work so unable to adapt it!! However, It looks much more efficient. I
suppose I should learn, never seem to have the time.

Iain
Jul 23 '05 #5
"Dr John Stockton" <sp**@merlyn.demon.co.uk> wrote in message
news:C2**************@merlyn.demon.co.uk...
JRS: In article <cd*******************@news.demon.co.uk>, seen in
news:comp.lang.javascript, Iain Downie <ia*********@bto.org> posted at
Thu, 15 Jul 2004 17:24:54 :
Signatures should not exceed four lines after a separator line minus
minus space. No-one reasonable would complain about your five text
lines, but the three empty lines and the rule are unnecessary.
Fair point. Will reduce.

Would "?" be better as a fuzzy indicator? Note that 'c' means 'circa'
means "about" and '+' means 'over'; they can be interpreted differently
by testing $1 & $3 separately.


Unfortunately, these are the two values that birdwatchers use, and yes, they
are meant to be differently interpreted. Our population trend stats will not
use this really, it is purely a personal record for folk entering records
online.

Iain
Jul 23 '05 #6

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

Similar topics

5
by: TG | last post by:
Dear PHP Group, I have two forms that are used to collect user information. The first one takes user inputted values such as fullname, city, address etc. I want these values to display in the...
3
by: Amir | last post by:
I'm looking for an example of form validation by JavaScript before it's processed by a PHP script. Any help will be appreciated.
3
by: D. Shane Fowlkes | last post by:
Sorry for the length of this post. I have created a rather complex form which has a header/line item (parent and child records) structure. It's for an intranet. A screenshot can be seen here: ...
4
by: moondaddy | last post by:
Is there a asp.net validator control that validates the length of the text being entered or does everyone just write jscript for this? -- moondaddy@nospam.com
1
by: Joel Barsotti | last post by:
Is there anything builtin to ASP.net that allows you to tie a text box to a button so when you press enter in the text box it emulates clicking a near by button. I've coded up some client side...
9
by: sellcraig | last post by:
Microsoft access 2 tables table "data main" contains a field called "code" table "ddw1" is created from a make table query of "data main" Goal- the data in "code" field in needs to...
5
by: Advo | last post by:
Basically, im redesigning a form page on our website. Currently the user submits the form, it does a few javascript checks and either submits to the "processstuff.php" page, or gives the user a...
11
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
3
by: acecraig100 | last post by:
I am fairly new to Javascript. I have a form that users fill out to enter an animal to exhibit at a fair. Because we have no way of knowing, how many animals a user may enter, I created a table...
6
by: smk17 | last post by:
I've spent the last few minutes searching for this question and I found an answer, but it wasn't quite what the client wanted. I have a simple online form where the user needs to fill out five...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.