@tf is almost there, I think. You can use JS on your widget’s admin form, as I’ve done it before (though not for validation).
Within your widget’s constructor, add an action:
add_action( "admin_print_scripts-widgets.php", array( __CLASS__, 'register_my_validation_script' ) );
Then create the corresponding function inside your widget’s class:
function register_my_validation_script() {
wp_enqueue_script( 'my-script-handle', plugins_url( '/my-validation-script.js', __FILE__ ), array( 'jquery' ), '1.0' );
}
This assumes your JS is a script (my-validation-script.js
) inside your plugin’s directory.
Within that JS script is your jQuery to execute your validation. This is where the pseudo code begins…
jQuery(document).ready(function($) {
$('.widget-control-save').on('click', function() {
// check this is the right widget - probably using something like $(this).closest('form').width-field...
// validate stuff
});
});