making a portable form-field data validator

  • Thread starter nntp-service.ohio-state.edu
  • Start date
N

nntp-service.ohio-state.edu

Hey folks -

I'm a newbie to java script. I'm trying to make a portable data-validator
for fields in an HTML form.

Ideally, it would work something like this:

<input type="text" name="test" onBlur=check_data(this.value, "date")>

The script would check the data against its data type (date, number, text)
and then spit out an error if the data wasn't the correct type.

I've seen scripts that check the data when the user hits the 'submit'
button. I would instead prefer to use the onblur event of each field.

To make it portable, it would then return focus to the field that the user
just entered data into.

Where I'm running into problems is referring to the field name. I've tried
referencing "this.parentnode.name", "this.parentnode.nodename",
"this.parentelement.name", etc. to no avail. I would like it to work with NN
6.x and IE5.x, but nowhere have I found a comprehensive guide to what
browser works with what javascript commands.

Can someone point me in the right direction?
 
L

Lasse Reichstein Nielsen

nntp-service.ohio-state.edu said:
I'm a newbie to java script. I'm trying to make a portable data-validator
for fields in an HTML form.

Ideally, it would work something like this:

<input type="text" name="test" onBlur=check_data(this.value, "date")>

You must put quotes around the onblur attribute value, i.e.,
<input type="text" name="test" onBlur="check_data(this.value, 'date')">

It contains at least one of the characters not allowed in unquoted
attribute values. I can't remember them all, so just quote all
attribute values and you are safe.
The script would check the data against its data type (date, number, text)
and then spit out an error if the data wasn't the correct type.

How do you recognize a correct date (I.e., what *is* a correct date
for your system)? What is correct text?
I've seen scripts that check the data when the user hits the 'submit'
button. I would instead prefer to use the onblur event of each field.

They do that for a reason! Users don't like to be interrupted while
they are typing.
To make it portable, it would then return focus to the field that the user
just entered data into.

Warning! If you capture the focus in this way, the user has only two
options if they want to change focus: write something correct or close
the browser. Capturing users like this can be very annying to them.
Where I'm running into problems is referring to the field name. I've tried
referencing "this.parentnode.name", "this.parentnode.nodename",
"this.parentelement.name", etc. to no avail.

Inside the function, "this" refers to the global object, not the input
element or the form. I don't know what "parentnode" should refer to,
do you mean "parentNode"?

If the function needs access to the form element, not just its value,
then you should pass the element as an argument to it. I.e.:

onblur="check_data(this, 'date')"

and then a check_data function:
---
function check_data(input,type) {
var success;
var value = input.value;
switch(type) {
case "date": // format yyyy/mm/dd
success = /^\d{4}\/\d{2}\/\d{2}$/.test(value);
break;
cast "number": // one or more digits
success = /^\d+$/.test(value);
break;
cast "text": // one or more digits are letters (or whatever)
success = /^\w+$/.test(value);
break;
default:
success = false; // what?
break;
}
if (!success) {
alert("Input named '"+input.name+"' incorrect. Must be "+type+"!");
input.style.border="2px solid red";
input.focus(); // but please don't do this!
} else {
input.style.border="";
}
return success
}
---
I would like it to work with NN
6.x and IE5.x, but nowhere have I found a comprehensive guide to what
browser works with what javascript commands.

What you need for this problem will work in all modern browsers. They
both support Javscript (i.e., ECMAScript) perfectly. The differences
are in the DOM. For forms, there are no significant differences.

/L
 
L

Lee

nntp-service.ohio-state.edu said:
Hey folks -

I'm a newbie to java script. I'm trying to make a portable data-validator
for fields in an HTML form.

Ideally, it would work something like this:

<input type="text" name="test" onBlur=check_data(this.value, "date")>

<input type="text" name="test" onChange="check_data(this, 'date')">

1. Don't validate onBlur. There are too many things that can
cause the onBlur handler to fire before the person is done
entering data into the field. As a simple example, they may
begin entering the date, and realize that they need to check
with a calendar application. There are other problems with
using onBlur, too.

2. Enclose the attribute value in quotes, using single quotes
for the second level.

3. By passing <this> instead of <this.value>, your check_data()
function will be able to access other attributes of the
form element, including its name, and will be able to set
focus to it directly, instead of by name.

Also, since you don't know that the person has Javascript enabled,
or if they may have chosen to bypass your client-side audits, you
should also audit the values on the server, if it's important that
they be correct. Auditing on the client side should only be for
the convenience of the visitor.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top