Form validation problem...

E

Eddie

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.
 
D

Douglas Crockford

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.
 
E

Eddie

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
 
K

kaeli

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
-------------------------------------------------
 
E

Eddie

Thank you!!

-eddie

kaeli said:
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
-------------------------------------------------
 
K

kaeli

Thank you!!

You're welcome.
And since you say it isn't for a class...

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;
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
-------------------------------------------------
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
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.
 
L

Lasse Reichstein Nielsen

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
 
R

Richard Hockey

Eddie said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top