I have a wordpress site (v.6.0.2) and I also have a contact form built with the WPForms plugin. What I am trying to do is to prevent the form from submitting when the name field does not contain two words (Name and Surname) in order to restrict spam messages.
In the functions.php I registered my script in the following way:
add_action( 'wp_enqueue_scripts', 'my_theme_scripts' );
function my_theme_scripts() {
wp_register_script('my_script', get_template_directory_uri() . '/js/my_script.js', array('jquery'),'1.1', true);
wp_enqueue_script( 'my_script' );
}
my_script.js in the js folder of my theme is:
$.noConflict();
jQuery(document).ready(function() {
$("#wpforms-form-2185").submit(function( event ) {
$("input#wpforms-2185-field_0").each(function() {
var words = $.trim($(this).val()).split(" ");
//alert(words.length);
if(words.length != 2) {
event.preventDefault();
}
});
});
});
What am I doing wrong and my form keeps submitting in every way I put names on?
Edit 1 My form gets submitted even with a single name word. These are the info messages from the Chrome console:
and the code from the formSubmit from the wpforms.min.js?ver=1.7.6:formatted:
formSubmit: function(e) {
e instanceof jQuery || (e = p(e)),
l.saveTinyMCE();
let t = WPFormsUtils.triggerEvent(e, "wpformsBeforeFormSubmit", [e]);
t.isDefaultPrevented() ? l.restoreSubmitButton(e, e.closest(".wpforms-container")) : e.hasClass("wpforms-ajax-form") && "undefined" != typeof FormData ? l.formSubmitAjax(e) : l.formSubmitNormal(e)
},
I am not a js expert but I think the above code is responsible for form submitting in any case.

