Form Validation Problem

Z

z24

Hello I am having a bit of a problem with validating a form. All I
want the form to do is if the user leaves a field empty for a
notification to display. The latest thing I have tried is to use
onchange like this

ONCHANGE=if(this.value.length < 1) alert('Please Enter your Name.')

This problem with this method is that it does do the trick as long as
you click into the field before you click submit. Does anyone know of
anyways to get around this problem. I do not want to make it too
complicated. If there is someway to do it like this could someone
please let me know.

Thank You in advance
 
I

Ivo

Hello I am having a bit of a problem with validating a form. All I
want the form to do is if the user leaves a field empty for a
notification to display. The latest thing I have tried is to use
onchange like this

ONCHANGE=if(this.value.length < 1) alert('Please Enter your Name.')

This problem with this method is that it does do the trick as long as
you click into the field before you click submit. Does anyone know of
anyways to get around this problem. I do not want to make it too
complicated. If there is someway to do it like this could someone
please let me know.

Don't check the input element on its own, but validate the whole form. An
ideal form contains no onchange, onkeypress or onwhatever event attributes,
except one:
onsubmit

This will work whichever way the user chooses to submit the form, by
clicking the submit button, hitting the Enter key on his keyboard, or any
other way. The simplest is perhaps to put the event handler in opening tag
of the form, like so:
<form action="..." method="..." onsubmit="validate(this)">

where "validate" is a javascript that takes care of all elements in the
form, and returns true or false depending on the input. The "this" word is a
reference to the form. If the function returns true, the form submits, if
false, you can alert a message and the form will not submit. For example:

function validate(f){
var els=f.elements, msg='';
if( !els['yourname'].value ){ msg+='The name field is empty.\n'; }
if( !els['youremail'].value ){ msg+='The email field is empty.\n'; }
if( msg ) {
alert(msg + 'The form has not been submitted.' );
els['yourname'].focus();
return false;
}
return true;
}

HTH
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Tue,
23 Nov 2004 18:19:04, seen in Ivo
Don't check the input element on its own, but validate the whole form. An
ideal form contains no onchange, onkeypress or onwhatever event attributes,
except one:
onsubmit


Not so.

It may be true for forms intended to collect information to be processed
elsewhere, but it is very possible to write javascript pages that
provide services to the reader without consulting the originating site.

Consider, for example, how utterly useless <URL:http://www.merlyn.demon.
co.uk/js-quick.htm> would be without its onClick code. Likewise js-
clndr.htm .
 
I

Ivo

Ivo posted :

Not so.

It may be true for forms intended to collect information to be processed
elsewhere, but it is very possible to write javascript pages that
provide services to the reader without consulting the originating site.


Sure, perhaps I should have been more clear and leave no doubt I was only
talking about ordinary forms, collecting ordinary user input and transmit
that in an ordinary way to the server. That is what the OP's question was
about.

And even less ordinary forms, intended to come alive with javascript, are
often better off with event handlers added later to the elements rather than
hardcoded in the HTML. So as to separate not only presentation and content,
but also content and behaviour. This may be a matter of habit and taste
though.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top