/    Sign up×
Community /Pin to ProfileBookmark

Javascript – onsubmit problem

Here’s the deal: I wrote a simple javascript validation:

[CODE]function userinformation(){
var message=”Please complete the following: nn”;
var noerrors=message;

var dayselected=document.topform.day.selectedIndex;
if(document.topform.day.options[dayselected].value==”null”){
message+=”Please select a dayn”;
}

if(document.topform.daterequested.value==””){
message+=”Please provide a requested tour daten”;
}

var numberattendinglist=document.topform.numberattending.selectedIndex;
if(document.topform.numberattending.options[numberattendinglist].value==”null”){
message+=”Please select a dayn”;
}

if(document.topform.firstname.value==””){
message+=”Please provide your first namen”;
}

if(document.topform.lastname.value==””){
message+=”Please provide your last namen”;
}

if(document.topform.address.value==””){
message+=”Please provide your addressn”;
}

if(document.topform.city.value==””){
message+=”Please provide your cityn”;
}

if(document.topform.state.value==””){
message+=”Please provide your staten”;
}

if(document.topform.zip.value==””){
message+=”Please provide your zip coden”;
}

if(document.topform.highschool.value==””){
message+=”Please provide your high schooln”;
}

if(document.topform.gradyear.value==””){
message+=”Please provide your graduation yearn”;
}

if(document.topform.phonenumber.value==””){
message+=”Please provide your phone numbern”;
}

if(message==noerrors){
return true;
}else{
alert(message);
return false;
}
}[/CODE]

On the form tag, I changed onsubmit to this:

[CODE]<form onsubmit=”return userinformation(this);”>[/CODE]

Why will this form only submit in FF? Any suggestions or help would be appreciated.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@toicontienJan 24.2007 — Can you post the full HTML for your FORM? And is Internet Explorer showing your error alert box or is it showing a JavaScript error?
Copy linkTweet thisAlerts:
@konithomimoJan 24.2007 — 
  • 1. You send the function an argument, but you dont have the function taking in an argument.


  • 2. Some browsers will require you to use "this.form" instead of just "this", even though you are in the form tag.


  • 3. Your code can be better written, for example, send the form and use it instead of document.topform, as well, this:


  • if(message==noerrors){
    return true;
    }else{
    alert(message);
    return false;
    }


    Could simply be written as:

    if(message==noerrors)
    return true;

    alert(message);
    return false;
    }
    Copy linkTweet thisAlerts:
    @ryanbutlerauthorJan 24.2007 — Some browsers will require you to use "this.form" instead of just "this", even though you are in the form tag.[/quote]

    That was the problem, thanks for the tip.

    Your code can be better written, for example, send the form and use it instead of document.topform, as well, this:[/quote]

    I don't understand what you mean by the first statement, document.topform? I need to validate via JavaScript and I thought document.name_of_form.form_field.value=="" was an appropriate way of doing it.
    Copy linkTweet thisAlerts:
    @konithomimoJan 24.2007 — It is an appropriate way of doing it, but if you send it the form, then you can simply use a variable name, which allows for shorter code.
    Copy linkTweet thisAlerts:
    @ryanbutlerauthorJan 24.2007 — Hmmm...this is aggravating...there's a problem somewhere with one of my drop down lists. Thanks for the help though, its appreciated.
    Copy linkTweet thisAlerts:
    @toicontienJan 25.2007 — If using this:
    [code=html]<form onsubmit="return userinformation(this);">[/code]
    Then change your javascript function thusly:
    <i>
    </i>function userinformation(form){
    var message="Please complete the following: nn";
    var noerrors=message;

    var dayselected=form.day.selectedIndex;
    if(form.day.options[dayselected].value=="null"){
    message+="Please select a dayn";
    }

    if(form.daterequested.value==""){
    message+="Please provide a requested tour daten";
    }

    var numberattendinglist=form.numberattending.selectedIndex;
    if(form.numberattending.options[numberattendinglist].value=="null"){
    message+="Please select a dayn";
    }

    if(form.firstname.value==""){
    message+="Please provide your first namen";
    }

    if(form.lastname.value==""){
    message+="Please provide your last namen";
    }

    if(form.address.value==""){
    message+="Please provide your addressn";
    }

    if(form.city.value==""){
    message+="Please provide your cityn";
    }

    if(form.state.value==""){
    message+="Please provide your staten";
    }

    if(form.zip.value==""){
    message+="Please provide your zip coden";
    }

    if(form.highschool.value==""){
    message+="Please provide your high schooln";
    }

    if(form.gradyear.value==""){
    message+="Please provide your graduation yearn";
    }

    if(form.phonenumber.value==""){
    message+="Please provide your phone numbern";
    }


    if(message==noerrors){
    return true;
    } else {
    alert(message);
    return false;
    }
    }

    Does that solve your problem? If not, please post the full HTML for the Web page. You also might drop by the W3C HTML validator to make sure no markup errors are giving you problems. http://validator.w3.org/
    Copy linkTweet thisAlerts:
    @ryanbutlerauthorJan 25.2007 — Sorry, it had nothing to do with the change of the function. It has to do with another JavaScript that someone helped me that required a little extra tweaking.

    All is well now, thanks again for the help.
    ×

    Success!

    Help @ryanbutler spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.1,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,
    )...