473,804 Members | 3,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.lengt h;
count = "";
if (count_val.char At(0) == "c"){
count = count_val.subst ring(1,len);
} else if (count_val.char At(len-1) == "+"){
count = count_val.subst ring(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 1287
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.lengt h;
count = "";
if (count_val.char At(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("a n_item.value".r eplace(/[^\d]/g,""));
if(!count){aler t("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.hi ddenFieldName.v alue=count;
return true;
}

Mick

{
count_val = an_item.value;
len = count_val.lengt h;
count = "";
if (count_val.char At(0) == "c"){
count = count_val.subst ring(1,len);
} else if (count_val.char At(len-1) == "+"){
count = count_val.subst ring(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.de mon.co.uk>, seen in
news:comp.lang. javascript, Iain Downie <ia*********@bt o.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.demo n.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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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.de mon.co.uk> wrote in message
news:C2******** ******@merlyn.d emon.co.uk...
JRS: In article <cd************ *******@news.de mon.co.uk>, seen in
news:comp.lang. javascript, Iain Downie <ia*********@bt o.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
4264
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 second form when it is called. Both forms are .htm files that call themselves when the submit button is press via the following command in each form: <form method="post" action="<?php $server?>">
3
1823
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
2569
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: http://www.drpt.virginia.gov/temp1.gif All the fields on this form have validation controls on them so they can not submit the form unless all fields are completed and some fall within a specified numeric range. When the form first is loaded,...
4
9911
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
1625
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 javascript that does this, but it fails when their is a validator monitoring the text box (in firefox, but that's 10% of my visitors).
9
3140
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 be inserted into a standard web address in the table (the filed name is link) in ddw1 Example address ---
5
1943
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 JS error message.. where they have to go back and fill the details in. Im trying to optimise and make this form/page as best as it can be. Do you think it would be a better idea to continue using JS or use JS then submit to the php page(where...
11
3004
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 certain fields match certain criteria, and inform the user in different ways when the data is wrong (offcourse, this will be checked on posting the data again, but that's something I've got a lot of experience with). Now, offcourse it's...
3
4014
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 with a createElement function to add additional entries. The table has the first row of input text boxes already in it. You have to click a button to add another row. That seems to be working fine. How do I pull the information from the input boxes...
6
1878
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 fields out of nine. The other four are already there and filled out for the user. When they hit submit, all data is sent to us. But, if they desire (for whatever reason) the user can possibly delete what is already there and fill in something...
0
9579
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10578
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10321
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9152
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.