Link to home
Start Free TrialLog in
Avatar of Mark
Mark

asked on

html form submit action skipping validation javascript function

I have the javascript/html shown in the code snippet below. It is a simple form. The only button on the form is set to run a validation script before submitting the form. The validation script attempts to insure that one or another value of the radio input mbrType is selected. Using both IE and firefox, I can't get this to ever fail, but occasionally some user manages to submit this form without selecting this option. Why? Possibly they are using some other browser. I am going to change the button onclick action to "return doSubmit()" which I probably should have had to begin with.

What about return from the function? should I end with:
   document.register.submit();
   return true;

or perhaps:

  return document.register.submit();

? This is so inermittant (and I cannot get it to fail myself) that I probably won't be able to test these syntaxes. So, some advice from EE on best-practice would be appreciated and applied and, hopefully, will fix the problem. THX

function doSubmit()
{
    var frm = document.register;
    var found = false;
 
    for (i = 0; i < frm.mbrType.length; i++)
        if (frm.mbrType[i].checked == true) found = true;
 
    if (found == false)
    {
        alert("Please select whether you are retired or still working for the SHP.");
        frm.mbrType[0].focus();
        return false;
    }
:
    document.register.submit();
}
:
<form name=register method=POST action="register.jsp">
<input type=radio name=mbrType value="PENSION">I retired from the ...
<input type=radio name=mbrType value="ACTIVE">I work for the ...
:
<button class=ohprsButton onclick="doSubmit();">Register Me</button>
</form>

Open in new window

Avatar of erikTsomik
erikTsomik
Flag of United States of America image

try replacing your form with this form
<form name="register" method="POST" action="register.jsp">
<input type="radio" name="mbrType" value="PENSION">I retired from the ...
<input type="radio" name="mbrType" value="ACTIVE">I work for the ...
<input type="button" class="ohprsButton" onclick="doSubmit();" value="Register Me">
</form>

That should do it
Avatar of Mark
Mark

ASKER

I've checked into this a bit and found the following at
http://www.w3schools.com/tags/tag_button.asp

Always specify the type attribute for the button. The default type for Internet Explorer is "button", while in other browsers (and in the W3C specification) it is "submit".

I did not do that, so maybe some other browser did the submit, but not the validation.

Does anyone else have comments on the difference/preference between using <button> versus <input type=button> ?
ASKER CERTIFIED SOLUTION
Avatar of erikTsomik
erikTsomik
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zvonko
Check this:
<script>
function doSubmit(frm){
    //var frm = document.register;
    var found = false;
 
    for (i = 0; i < frm.mbrType.length; i++)
        if (frm.mbrType[i].checked == true) found = true;
 
    if (found == false)
    {
        alert("Please select whether you are retired or still working for the SHP.");
        frm.mbrType[0].focus();
        return false;
    }
 
    return true;
}
</script>
<form name=register method=POST action="register.jsp" onSubmit="return doSubmit(this)" >
<input type=radio name=mbrType value="PENSION">I retired from the ...
<input type=radio name=mbrType value="ACTIVE">I work for the ...
:
<input type="submit" class=ohprsButton value="Register Me" >
</form>

Open in new window

Avatar of Mark

ASKER

This link was useful it describing my problem. In fact, the  was behaving as a submit and I needed to say