Field validation - based on other...

User 2582728 Photo


Registered User
48 posts

I'm really at a loss. I must have read a couple of hundred articles on jquery, form "submit" events, etc. Still cannot get the form processing to work after I block submission the first time due to user input error.

Here's the latest version:

http://stronggroup.com/test/analysis.htm

The "outer" web page loads the html web form generated by WFB. At the tail end of the load, after the form is loaded, my jquery form handler for the form's "submit" method is defined. It appears to work, and reaches the end of the code on either a successful entry (after which it displays the results page with graphs), or blocks the transition to the results page on user error -- this accomplished by having the form's event handler return "false".

BUT...as before, once it has returned "false", I cannot get the form (maybe the button?) to be properly enabled again, and the user can't trying submitting again with fixed entries.

My jquery handler:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script>
$(document).ready(function(){
$('#selectedTarget').load('hlv_calc_form.html', function() {

$("form").submit(function(){
var errmsg = '';
var result = true;
var yourage = parseInt($('#item2_number_1').val());
var numkids = parseInt($('#item20_select_1').val());

alert('Entered On Submit handler');

// compare Age is greater than or equal to Retirement Age
if ( yourage >= $('#item4_number_1').val() ) {
errmsg += 'Age must be less than retirement age\n';
};

// If married, and...
if (
( $('input[name=married]:radio:checked').val() === 'Yes') &&
// ...Spouse Age is greater than or equal to Spouse Retirement Age
( $('#item17_number_1').val() >= $('#item13_number_1').val() )
)
{
errmsg += 'Spouse age must be less than spouse retirement age\n';
};

// Must be older than any children (purposely dropping through subsequent cases)
switch (numkids)
{
case 6:
if ( yourage < ($('#item34_number_1').val()) ) {
errmsg += 'Your 6th child appears to be older than you\n';
}
case 5:
if ( yourage < ($('#item33_number_1').val()) ) {
errmsg += 'Your 5th child appears to be older than you\n';
}
case 4:
if ( yourage < ($('#item32_number_1').val()) ) {
errmsg += 'Your 4th child appears to be older than you\n';
}
case 3:
if ( yourage < ($('#item31_number_1').val()) ) {
errmsg += 'Your 3rd child appears to be older than you\n';
}
case 2:
if ( yourage < ($('#item30_number_1').val()) ) {
errmsg += 'Your 2nd child appears to be older than you\n';
}
case 1:
if ( yourage < ($('#item23_number_1').val()) ) {
errmsg += 'Your 1st child appears to be older than you\n';
}
};

if (errmsg.length > 0) {
errmsg = 'Please fix the following and try again:\n\n' + errmsg;
alert(errmsg);
result = false;
}
else {
alert('no errors!');
result = true;
}
alert('about to return a result = ' + result);
return result;
});
});
});
</script>


If anyone is curious, you can look at the "analysis" page of the website above. If you open the html in an editor, you can scroll down to a Load event where the "hlv_calc_form" is loaded into a div on the page, followed by the javascript above being loaded as well.

I'm hoping somebody has time, patience and some experience with this to share! I could really use another pair of eyes on this, as I really seem stuck right now. I know, btw, that the general technique -- returning false or true in a form's event handler -- is a perfectly valid approach, and works on many simple cases and demos. I'm guessing something is going on in the innards of WFB that I just don't know about, that is mucked up when I return "false" in the submit event handler.

Thx in advance for any perspective on this!
User 187934 Photo


Senior Advisor
20,271 posts

How about tying a focus event to the submit button instead of clicking it.
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 187934 Photo


Senior Advisor
20,271 posts

It seem to work for me.:lol:
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 187934 Photo


Senior Advisor
20,271 posts

I did it again a got the submit button locked up. I see it appears to be disabled by the form builders own validation. data-disabled="disabled">
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 2582728 Photo


Registered User
48 posts

Interesting... I wonder if I can get away with re-enabling it myself, maybe at the top of my validation routine? I'm guessing that perhaps there's a mechanism to prevent double-clicks that might result in a double order, for example, in an e-commerce application, and maybe my attempt to disable prevent form submission when it doesn't validate is messing that up.
User 187934 Photo


Senior Advisor
20,271 posts

I looked at it closer and I think your code is actually disabling the submit button.
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 2582728 Photo


Registered User
48 posts

Not sure where you're seeing that... the "analysis" page (where the form is) is constructed with no awareness of the form, except for a large empty DIV that I leave in place as a placeholder for the form. When the page loads, it invokes the javascript that I included in this thread -- this is the top few lines of jquery that wait for the page to be Ready, and then Load the form's HTML created by WFB into the empty placeholder on the page.

The jquery that I included here is, except for those top few lines, solely the form validation logic that's bound to the Submit event on the form. Most of that code looks at the various fields on the form and concatenates error messages (with intervening new-lines) to a string. At the very bottom of the script you'll see:

if (errmsg.length > 0) {
errmsg = 'Please fix the following and try again:\n\n' + errmsg;
alert(errmsg);
result = false;
}
else {
alert('no errors!');
result = true;
}
alert('about to return a result = ' + result);
return result;


If the error msg string (length = 0), I pop up an alert saying, "no errors!" (just for diagnostics right now), and then exit, returning "true" as the result of the form submit event handler. If the error msg string is longer than 0, I display it in an alert, and then set the form handler's result to "false". In the "on submit" event, a handler's return of "false" should cancel the submit.

The problem is that after cancelling it like this (which from the dozens of articles I've read is a standard way to do this -- at least on a hand-coded form), the form somehow remains in a disabled state, and I haven't found a way to "revive" it.

If there's another way to do my form-level validation - and block the submit if there are errors - that works better with WFB, I'll be happy to give it a try!
User 187934 Photo


Senior Advisor
20,271 posts

Did you make any changes to your code?, because now I can't get the submit button locked on error. It lets me keep clicking the submit.
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 2582728 Photo


Registered User
48 posts

no...no changes at all since I uploaded the version at: stronggroup.com/test/

Weird...I'll take a look...
User 2582728 Photo


Registered User
48 posts

I just managed to "break it"... The thing that breaks it is when one of my "form level" validations fail. You'll see them under any of these conditions:

- You set your Age as equal to or greater than your Retirement Age
- you set your Spouse Age equal or greater than Spouse Retirement Age
- You set any of your 1 to 6 children to be older than you (e.g., you're 20, and your kids are set to age 21 or 22).

If any of THOSE errors occur, my script catches them, and returns "false" so the form isn't submitted to the php page that does the calculations and displays the graphic output.

Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.