473,382 Members | 1,362 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,382 software developers and data experts.

Form validation problem...

I have a form that's used to sort a series of items. The form has a number
of text fields. Each text field should contain a number. When the form is
submitted I would like to do two things:

1. Verify that only numbers are entered in each text field.
2. Verify that there are no duplicate entries in any text field.

So each field should be a unique number. Another thing complicating this is
that I do not know how many fields I will have on any given form. I am
building this form dynamically from a database call. So the function would
need to parse through the form and check each field, without specifying the
name of the field...

I realize this may be simple for most of you (I'm hoping...), but I'm WAY
rusty on my Javascript, having spent the past few years doing mostly
server-side coding...

THANKS for your help!!!

E.
Jul 20 '05 #1
9 4566
> I have a form that's used to sort a series of items. The form has a number
of text fields. Each text field should contain a number. When the form is
submitted I would like to do two things:

1. Verify that only numbers are entered in each text field.
2. Verify that there are no duplicate entries in any text field.

So each field should be a unique number. Another thing complicating this is
that I do not know how many fields I will have on any given form. I am
building this form dynamically from a database call. So the function would
need to parse through the form and check each field, without specifying the
name of the field...


What class is this for, Eddie? I hope you are taking it pass/fail.

Jul 20 '05 #2
This isn't for a class. I'm building a small content management system for
my company's intranet and need to create a form that sorts child nodes.
I've been doing server-side coding (ASP,PHP,JSP) for a few years, but
haven't done any JS coding in a LONG time. So please excuse my ignorance...
Can you help?

eddie

"Douglas Crockford" <no****@laserlink.net> wrote in message
news:bj**********@sun-news.laserlink.net...
I have a form that's used to sort a series of items. The form has a number of text fields. Each text field should contain a number. When the form is submitted I would like to do two things:

1. Verify that only numbers are entered in each text field.
2. Verify that there are no duplicate entries in any text field.

So each field should be a unique number. Another thing complicating this is that I do not know how many fields I will have on any given form. I am
building this form dynamically from a database call. So the function would need to parse through the form and check each field, without specifying the name of the field...


What class is this for, Eddie? I hope you are taking it pass/fail.

Jul 20 '05 #3
In article <20*******************@news.newsreader.com>, ed***@msn.com
enlightened us with...
I have a form that's used to sort a series of items. The form has a number
of text fields. Each text field should contain a number.
parseInt
When the form is
submitted I would like to do two things:

1. Verify that only numbers are entered in each text field.
..length
2. Verify that there are no duplicate entries in any text field.
put in array and look for matches
So each field should be a unique number. Another thing complicating this is
that I do not know how many fields I will have on any given form.


loop through form.elements[] array
-------------------------------------------------
~kaeli~
Press any key to continue or any other key to quit.
Who is General Failure and why is he reading
my hard disk?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
Jul 20 '05 #4
Thank you!!

-eddie

"kaeli" <in********************@NOSPAMatt.net> wrote in message
news:MP************************@nntp.lucent.com...
In article <20*******************@news.newsreader.com>, ed***@msn.com
enlightened us with...
I have a form that's used to sort a series of items. The form has a number of text fields. Each text field should contain a number.


parseInt
When the form is
submitted I would like to do two things:

1. Verify that only numbers are entered in each text field.


.length
2. Verify that there are no duplicate entries in any text field.


put in array and look for matches
So each field should be a unique number. Another thing complicating this is that I do not know how many fields I will have on any given form.


loop through form.elements[] array
-------------------------------------------------
~kaeli~
Press any key to continue or any other key to quit.
Who is General Failure and why is he reading
my hard disk?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------

Jul 20 '05 #5
In article <20*******************@news.newsreader.com>, ed***@msn.com
enlightened us with...
Thank you!!


You're welcome.
And since you say it isn't for a class...
I have a form that's used to sort a series of items. The form has a number of text fields. Each text field should contain a number.


Here's my validation function.
function checkNumeric(strObject)
{
/* Returns true if the field has all numeric characters, false if
not.
You must pass in a input (text) object, not the value. */
var index;
var myChar;
/* add chars to the following string if you want punctuation or
spaces allowed */
var allowedChars = "0123456789"

if (! strObject)
{
return false;
}
else
{
var str = strObject.value;
}

for (index = 0; index < str.length; index ++)
{
myChar = str.charAt(index);
if (allowedChars.indexOf(myChar) == -1)
{
return false;
}
}
return true;
}

loop through form.elements[] array


x = document.formName.elements.length;
for (i=0; i<x; i++)
{
e = document.formName.elements[i];
if (e.type == "text")
{
// do something
}
}
-------------------------------------------------
~kaeli~
Press any key to continue or any other key to quit.
Who is General Failure and why is he reading
my hard disk?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
Jul 20 '05 #6
JRS: In article <20*******************@news.newsreader.com>, seen in
news:comp.lang.javascript, Eddie <ed***@msn.com> posted at Fri, 5 Sep
2003 10:05:23 :-
I have a form that's used to sort a series of items. The form has a number
of text fields. Each text field should contain a number. When the form is
submitted I would like to do two things:

1. Verify that only numbers are entered in each text field.
Firstly you must define "number". I presume that you do not mean
"decimal digit". Can it be signed? Can it have a decimal point, dot or
comma? Can it have thousands separators, comma or dot? Can it have an
exponent, as in 3e5? Must it be integer?

If, as seems probable, it must be a decimal digit string, then check it
with either of the RegExps /^\d+$/ or /\D/ - though the latter accepts
an empty string.
2. Verify that there are no duplicate entries in any text field.

So each field should be a unique number. Another thing complicating this is
that I do not know how many fields I will have on any given form.


Assuming that the number of fields is not ludicrously large :

(1) Do var Them = [] // possibly superfluous
(2) Do var T, U // subtle
(3) For each field found, then do
T = field.value // or +field.value
if (Them[T]!=U) ... reject, otherwise
Them[T] = 0

Possibly != should be !==; test or think.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #7
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:

I agree except for details of style.
(1) Do var Them = [] // possibly superfluous
You don't use that Them is an array, so you might as well just use an
object (purely to satisfy the K.I.S.S. principle), i.e.,
var Them = {};
or
var Them = new Object(); // for older browsers
(2) Do var T, U // subtle
You use this to make U be the undefined value, most likely for compatability
with old browsers.

I would much rather set the global variable "undefined" once an for all:
window.undefined = window.undefined;
This is safe if undefined is already defined, and defines it if not.
(3) For each field found, then do
T = field.value // or +field.value
if (Them[T]!=U) ... reject, otherwise
Them[T] = 0

Possibly != should be !==; test or think.


Change to "Them[T] = true;" and you won't even have to think :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #8

"Eddie" <ed***@msn.com> wrote in message
news:20*******************@news.newsreader.com...
I have a form that's used to sort a series of items. The form has a number of text fields. Each text field should contain a number. When the form is submitted I would like to do two things:

1. Verify that only numbers are entered in each text field.
Try regular expressions:

<script type="text/javascript">
<!--
// set up regular expression to accept only numeric expressions made from
the digits 0 - 9
numericRE=/^[0-9]+&/;

function NumericValidate(fieldObject)
{
// test to see if contents of field match regular expression using
RE.test(string) method
if(!numericRE.test(fieldObject.value))
{
alert('you have entered non-numeric characters in a numeric field. Please
enter numbers only in this field.');
fieldObject.focus();
return false;
}

// field is numeric
return true;
}
-->
</script>
You could call this function from within another function when validating
the entire form before submitting, or on a 'onblur' event in the field
itself

<input type="text" name="accesscode" onblur="NumericValidate(this);">
2. Verify that there are no duplicate entries in any text field.

So each field should be a unique number. Another thing complicating this is that I do not know how many fields I will have on any given form. I am
building this form dynamically from a database call. So the function would need to parse through the form and check each field, without specifying the name of the field...

I realize this may be simple for most of you (I'm hoping...), but I'm WAY
rusty on my Javascript, having spent the past few years doing mostly
server-side coding...

THANKS for your help!!!

E.

Jul 20 '05 #9
"Richard Hockey" <ri***********@dsl.pipex.com> writes:
Try regular expressions:

<script type="text/javascript">
<!--
HTML comments are not needed in Javascript.
// set up regular expression to accept only numeric expressions made from
the digits 0 - 9
numericRE=/^[0-9]+&/;


I think the "&" should be a "$".

For brevity, "[0-9]" can be replaced by "\d".

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #10

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

Similar topics

21
by: Stefan Richter | last post by:
Hi, after coding for days on stupid form validations - Like: strings (min / max length), numbers(min / max value), money(min / max value), postcodes(min / max value), telefon numbers, email...
4
by: bnp | last post by:
Hi All, I am quite new the JavaScript. Basically I am a C++ programmer, but now I am working on JavaScript since last 5 days. I have a problem regarding the form validation. I have created a...
6
by: Darren | last post by:
I have a form that has 10 fields on it. I have made all of them "Required". I also am using vb if statements to decide whether or not each field should be on the page. I am using the vb to...
9
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
7
by: h7qvnk7q001 | last post by:
I'm trying to implement a simple server-side form validation (No Javascript). If the user submits a form with errors, I want to redisplay the same form with the errors highlighted. Once the form...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
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...
4
by: Greg Scharlemann | last post by:
I'm trying to setup a dyamic dropdown list that displays a number of text fields based on the selected number in the dropdown. The problem I am running into is capturing the data already entered...
10
by: gweasel | last post by:
What is the best way to apply a Validation Rule - or rather, where is the best place to put it? Is there an advantage to putting it on the field in the table vs setting the validation rule on the...
5
by: lucyh3h | last post by:
Hi, I am trying to use XMLHttpRequest to do server side validation. I have several fields on a form and a submit button. The submit button has an event assocated with it when clicked. The...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.