Has anyone got form.showErrorMessage to work? | Community
Skip to main content
July 11, 2014
Solved

Has anyone got form.showErrorMessage to work?

  • July 11, 2014
  • 6 replies
  • 2573 views
I'm trying to implement custom validation on my embedded Marketo Form 2.0.  We're strictly B2B and so I am wanting to add some additional validation to the Email field - to ensure they aren't using typical ISP/ free email accounts.  I have tried setting the field type to String (rather than email) and setting/ emptying the Custom Validation message (I'd rather avoid this as the message will change according to logic in the javascript), but whatever I do the error message is not populated from my code:

form.onValidate(function(){
    var vals = form.getValues();
    if (jqff.inArray(vals.Email, invalidEmailDomains) > -1 ){
       console.log("invalid email");  //triggers OK
       form.submitable(false);
       var TextField_Email = form.getFormElem().find("#Email");
       console.log(TextField_Email); //gets the field OK
       form.showErrorMessage("Custom error message here", TextField_Email);
}

TIA
Ben
 
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by
Have you seen example #9 on this page:

http://developers.marketo.com/documentation/websites/forms-2-0#examples

I would follow that example. Replace vals.Country to vals.Email, and then use regex to match non-business emails. 

Let me know if that makes sense?

6 replies

Accepted solution
July 11, 2014
Have you seen example #9 on this page:

http://developers.marketo.com/documentation/websites/forms-2-0#examples

I would follow that example. Replace vals.Country to vals.Email, and then use regex to match non-business emails. 

Let me know if that makes sense?
July 11, 2014
Hi Murtza,
This is based on that example - hence the use of form.onValidate() and var vals = form.getValues();  My question is why is this line:

  form.showErrorMessage("Custom error message here", TextField_Email);

...not working?  As the comments in my code state - the error condition is being triggered and the variable TextField_Email is selected OK, but the validation message is not getting set to the string I am passing ("Custom error message here") its just displaying what is set in the form field's settings.

BTW jqff is just jQuery in noconflicts mode so don't let that confuse anyone...
July 11, 2014
Also whoever marked this as solved it isn't please remove the Solved flag.  Thanks
July 11, 2014
Looks like Enget Dang's done it - although I don't quiet see the difference to what I've got... will test & confirm...
July 11, 2014
Ben - Can you please post a link to your test page, or on JSfiddle? It would help to see code in context to figure out what's going on. 
July 31, 2014
For what it is worth, we are trying to do the same thing.  We are able to get the error message to show

var emailElem = form.getFormElem().find("#Email");
...{other logic, such as setting correct error_msg}...
 form.showErrorMessage(error_msg, emailElem);

----------
However, we have a different issue.  When the user clicks the submit button, everything works great - the error message is shown and the form is not submitted.  When user hits the Enter key instead, the form is not submitted but the error message shows briefly and then disappears.

It seems that ENTER is causing the form to reset, and thus prevents the error message from persisting. 

Any ideas on how to keep the error message persisting if a user clicks ENTER instead of clicking the submit button?