Form Validation - do not allow numbers | Community
Skip to main content
Jason_Hamilton1
Level 6
August 16, 2016
Solved

Form Validation - do not allow numbers

  • August 16, 2016
  • 1 reply
  • 2804 views

Does anyone have a sample form validation script that will not allow numbers to be entered in the first or last name field?

Thanks,

Jason

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 SanfordWhiteman

Unfortunately, the example code has a bug (always has). It doesn't correctly check against built-in validation, which can end up being very confusing when writing custom validators.

Something more extensible: MktoForms2 :: Extended Validation from Validators Array

1 reply

August 16, 2016

Hi @Jason Hamilton​

<script>

MktoForms2.loadForm("//app-ab99.marketo.com", "999-IBN-999", 9999, function (form) {

    //listen for the validate event

    form.onValidate(function() {

        // Get the values

        var vals = form.vals();

        //Check your condition

        if (vals.FirstName.match(/[0-9]+/) || vals.LastName.match(/[0-9]+/)) {

            // Prevent form submission

            form.submittable(false);

            // Show error message, pointed at VehicleSize element

            form.showErrorMessage("I don't like numbers!");

        }

        else {

            // Enable submission for those who met the criteria

            form.submittable(true);

        }

  });

});

</script>

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

This code is based on example 9

SanfordWhiteman
SanfordWhitemanAccepted solution
Level 10
August 16, 2016

Unfortunately, the example code has a bug (always has). It doesn't correctly check against built-in validation, which can end up being very confusing when writing custom validators.

Something more extensible: MktoForms2 :: Extended Validation from Validators Array

Jason_Hamilton1
Level 6
August 25, 2016

Worked great as usual, thanks @Sanford Whiteman​