Form Validation Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • z24

    Form Validation Problem

    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(thi s.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
  • Ivo

    #2
    Re: Form Validation Problem

    "z24" wrote[color=blue]
    > 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(thi s.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.[/color]

    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="valid ate(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



    Comment

    • Dr John Stockton

      #3
      Re: Form Validation Problem

      JRS: In article <41a36f43$0$765 05$b83b6cc0@new s.wanadoo.nl>, dated Tue,
      23 Nov 2004 18:19:04, seen in news:comp.lang. javascript, Ivo
      <no@thank.you > posted :[color=blue]
      >
      >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[/color]


      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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

      Comment

      • Ivo

        #4
        Re: Form Validation Problem

        "Dr John Stockton" wrote[color=blue]
        > Ivo posted :[color=green]
        > >
        > >Don't check the input element on its own, but validate the whole form. An
        > >ideal form contains no onchange, onkeypress or onwhatever event[/color][/color]
        attributes,[color=blue][color=green]
        > >except one:
        > > onsubmit[/color]
        >
        > 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.[/color]


        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


        Comment

        Working...