cancel a function

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

    #1

    cancel a function

    Hi, I have a form. I have the code, below, to check for certain compulasry
    fields. The Submit button has:

    onClick="valida te(this.form)"

    My problem is that the warnings come up fine, but the form still gets
    posted. Please can someone advise how I cancel the function if data is
    missing?

    Thank. Jon

    <SCRIPT language="JavaS cript" type="text/javascript">
    <!--
    function validate(x) {
    var e = "";
    var r = true;

    if(x.txtName.va lue == "") {
    e += "\n\nPlease enter a name";
    r = false;
    }

    if(x.txtCompany .value == "") {
    e += "\n\nPlease enter a company name";
    r = false;
    }
    if(x.txtPhone.v alue == "") {
    e += "\n\nPlease enter a phone number";
    r = false;
    }
    if(x.txtEmail.v alue == "") {
    e += "\n\nPlease enter an email address";
    r = false;
    }
    if(r == false) {
    alert("The following must be completed first:"+e);
    }
    return r;
    }
    // -->
    </SCRIPT>


  • Lasse Reichstein Nielsen

    #2
    Re: cancel a function

    "Jon" <jon@SPAM_OFFth eexperts.co.uk> writes:
    [color=blue]
    > Hi, I have a form. I have the code, below, to check for certain compulasry
    > fields. The Submit button has:
    >
    > onClick="valida te(this.form)"[/color]

    You mean
    onclick="return validate(this.f orm);"

    I prefer to put it on the form tag:
    <form ... onsubmit="retur n validate(this); ">
    [color=blue]
    > if(r == false) {[/color]

    Pet peeve: don't compare to a boolean. Just write
    if(!r) { ...


    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • McKirahan

      #3
      Re: cancel a function

      "Jon" <jon@SPAM_OFFth eexperts.co.uk> wrote in message
      news:brtaho$kf8 $1@hercules.bti nternet.com...[color=blue]
      > Hi, I have a form. I have the code, below, to check for certain[/color]
      compulasry[color=blue]
      > fields. The Submit button has:
      >
      > onClick="valida te(this.form)"
      >
      > My problem is that the warnings come up fine, but the form still gets
      > posted. Please can someone advise how I cancel the function if data is
      > missing?
      >
      > Thank. Jon
      >
      > <SCRIPT language="JavaS cript" type="text/javascript">
      > <!--
      > function validate(x) {
      > var e = "";
      > var r = true;
      >
      > if(x.txtName.va lue == "") {
      > e += "\n\nPlease enter a name";
      > r = false;
      > }
      >
      > if(x.txtCompany .value == "") {
      > e += "\n\nPlease enter a company name";
      > r = false;
      > }
      > if(x.txtPhone.v alue == "") {
      > e += "\n\nPlease enter a phone number";
      > r = false;
      > }
      > if(x.txtEmail.v alue == "") {
      > e += "\n\nPlease enter an email address";
      > r = false;
      > }
      > if(r == false) {
      > alert("The following must be completed first:"+e);
      > }
      > return r;
      > }
      > // -->
      > </SCRIPT>[/color]


      Change it to the following; watch for word-wrap:

      Add " onSubmit="retur n validate(this.f orm)" to your "form" tag
      and remove the "onClick=" from the Submit button.


      Comment

      • Jon

        #4
        Re: cancel a function

        Don't worry - sorted it.
        The button was still a submit button, and I've added in a form submit at the
        end of this script

        Jon
        "Jon" <jon@SPAM_OFFth eexperts.co.uk> wrote in message
        news:brtaho$kf8 $1@hercules.bti nternet.com...
        Hi, I have a form. I have the code, below, to check for certain compulasry
        fields. The Submit button has:

        onClick="valida te(this.form)"

        My problem is that the warnings come up fine, but the form still gets
        posted. Please can someone advise how I cancel the function if data is
        missing?

        Thank. Jon

        <SCRIPT language="JavaS cript" type="text/javascript">
        <!--
        function validate(x) {
        var e = "";
        var r = true;

        if(x.txtName.va lue == "") {
        e += "\n\nPlease enter a name";
        r = false;
        }

        if(x.txtCompany .value == "") {
        e += "\n\nPlease enter a company name";
        r = false;
        }
        if(x.txtPhone.v alue == "") {
        e += "\n\nPlease enter a phone number";
        r = false;
        }
        if(x.txtEmail.v alue == "") {
        e += "\n\nPlease enter an email address";
        r = false;
        }
        if(r == false) {
        alert("The following must be completed first:"+e);
        }
        return r;
        }
        // -->
        </SCRIPT>



        Comment

        Working...