Form Validation Problem

Discussion in 'Javascript' started by z24, Nov 23, 2004.

  1. z24

    z24 Guest

    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
    z24, Nov 23, 2004
    #1
    1. Advertising

  2. z24

    Ivo Guest

    "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
    Ivo, Nov 23, 2004
    #2
    1. Advertising

  3. JRS: In article <41a36f43$0$76505$>, dated Tue,
    23 Nov 2004 18:19:04, seen in news:comp.lang.javascript, 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.

    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.
    Dr John Stockton, Nov 23, 2004
    #3
  4. z24

    Ivo Guest

    "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
    Ivo, Nov 24, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Basterfield

    Web form validation vs object validation

    Colin Basterfield, Nov 28, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    425
    Tommy
    Nov 29, 2003
  2. Sumith Mathur
    Replies:
    0
    Views:
    2,203
    Sumith Mathur
    Sep 23, 2004
  3. OriginalBrownster

    string validation/ form validation

    OriginalBrownster, Aug 21, 2006, in forum: Python
    Replies:
    1
    Views:
    421
    Simon Forman
    Aug 21, 2006
  4. Grigory Temchenko
    Replies:
    0
    Views:
    328
    Grigory Temchenko
    Dec 28, 2008
  5. bnp
    Replies:
    4
    Views:
    320
Loading...

Share This Page