[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 907
  • Last Modified:

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

0
jmarkfoley
Asked:
jmarkfoley
  • 2
  • 2
1 Solution
 
erikTsomikSystem Architect, CF programmer Commented:
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
0
 
jmarkfoleyAuthor Commented:
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> ?
0
 
erikTsomikSystem Architect, CF programmer Commented:
read this. It will tell you the difference
http://particletree.com/features/rediscovering-the-button-element/
0
 
ZvonkoSystems architectCommented:
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

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now