JavaScript issue

Hi,

not really a FW related issue, but maybe someone can give me a hint.

I have created a text link that should call a JavaScript function to verify form input (external script) and then submit the form.

This is what I used as link:

javascript:if(CheckForm() == ‘true’){document.forms[0].submit();};

The CheckForm function is an external script that works (I had it used in a form before). It shows an alert message when someone wents wrong and then returns false.

My idea is to call submit only if form validation was successful. Anyway, the form is sent regardless what CheckForm returns. Where is my error here?

By the way, this is the CheckForm function (to be complete):

function CheckForm()
{
var f = document.forms[0];

if (f.elements["emailaddress"].value == "")
{
        alert("Error:nPlease fill in the email address field.");
        return false;
  };};

Thanks for your efforts. :slight_smile:
Tom


freewaytalk mailing list
email@hidden
Update your subscriptions at:

Can you show an example page where this fails? There’s something odd about this construction, and I can’t figure it out from your example. A link within a form should not submit that form – that’s a hazardous construction. If you want to validate a form, you should attach a listener to the submit event on that form, then your script can properly intercept the form at the moment it is being submitted and validate its contents.

Walter

On Jul 6, 2016, at 5:04 AM, Tom email@hidden wrote:

Hi,

not really a FW related issue, but maybe someone can give me a hint.

I have created a text link that should call a JavaScript function to verify form input (external script) and then submit the form.

This is what I used as link:

javascript:if(CheckForm() == ‘true’){document.forms[0].submit();};

The CheckForm function is an external script that works (I had it used in a form before). It shows an alert message when someone wents wrong and then returns false.

My idea is to call submit only if form validation was successful. Anyway, the form is sent regardless what CheckForm returns. Where is my error here?

By the way, this is the CheckForm function (to be complete):

function CheckForm()
{
var f = document.forms[0];

if (f.elements[“emailaddress”].value == “”)
{
alert(“Error:nPlease fill in the email address field.”);
return false;
};};

Thanks for your efforts. :slight_smile:
Tom


freewaytalk mailing list
email@hidden
Update your subscriptions at:
Information for existing FreewayTalk / Groups.io users - Site Feedback - Softpress Talk


freewaytalk mailing list
email@hidden
Update your subscriptions at:
https://freewaytalk.softpress.com/person/options