Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Form Validation Problem

Reply
Thread Tools

Form Validation Problem

 
 
z24
Guest
Posts: n/a
 
      11-23-2004
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
 
Reply With Quote
 
 
 
 
Ivo
Guest
Posts: n/a
 
      11-23-2004
"z24" wrote
> 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
--
Ivo



 
Reply With Quote
 
 
 
 
Dr John Stockton
Guest
Posts: n/a
 
      11-23-2004
JRS: In article <41a36f43$0$76505$(E-Mail Removed)>, dated Tue,
23 Nov 2004 18:19:04, seen in news:comp.lang.javascript, Ivo
<(E-Mail Removed)> posted :
>
>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 .

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
Ivo
Guest
Posts: n/a
 
      11-24-2004
"Dr John Stockton" wrote
> Ivo posted :
> >
> >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.



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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A form validation library with javascript validation Grigory Temchenko Python 0 12-28-2008 08:38 PM
string validation/ form validation OriginalBrownster Python 1 08-21-2006 01:50 AM
Struts validation for combine form fields validation Sumith Mathur Java 0 09-23-2004 06:26 PM
Form Validation Problem...Persisiting form fields on validation failure. bnp Javascript 4 05-12-2004 12:16 PM
Web form validation vs object validation Colin Basterfield ASP .Net 1 11-29-2003 12:10 AM



Advertisments