SOLVED

Form Validation - do not allow numbers

Go to solution
Jason_Hamilton1
Level 8 - Champion Alumni

Form Validation - do not allow numbers

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

Tags (3)
1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Form Validation - do not allow numbers

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

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Re: Form Validation - do not allow numbers

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
Level 10 - Community Moderator

Re: Form Validation - do not allow numbers

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 8 - Champion Alumni

Re: Form Validation - do not allow numbers

Worked great as usual, thanks Sanford Whiteman

Jason_Hamilton1
Level 8 - Champion Alumni

Re: Form Validation - do not allow numbers

I have another question re integrating corporate email validation into this, but I will start a new string.