Ahoy! @Francois I also tried field validation rules with regex and didn’t have much luck!
Below is the JavaScript method. When a white space is added to field_xx, it will automatically erase it on keyup. Hope that helps.
$(document).on('knack-view-render.any', function(event, scene) {
// Prevent whitespace input in a text field with key 'field_xx
$('#field_xx').on('input', function() {
this.value = this.value.replace(/\s/g, '');
});
});
@Francois I agree, it is unfortunate that there’s no method of doing so natively.
The reason its not working for you example is because name fields are split into separate inputs, so you have to target those inputs differently.
Here is the updated code to cover any inputs in your name field (i.e. first, middle, last, etc), replace field_x with your name field:
$(document).on('knack-view-render.any', function(event, scene) {
// Prevent whitespace input in a name field with key 'field_58'
$('#kn-input-field_xx input').on('input', function() {
this.value = this.value.replace(/\s/g, '');
});
});
If you wanted to only target one of the fields (e.g. the first name), you can swap out the event handler to this: