By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,442 Members | 1,302 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,442 IT Pros & Developers. It's quick & easy.

Form validation problem...

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
> 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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.