Prevent invalid or empty values from being saved to the database and retain the form field values upon error

You’d do it something like this:

function sanitize_number_callback ($input){

    if( !preg_match( '/...regex for valid here.../', $input ) ){
        add_settings_error(
            'my_option',
            esc_attr( 'my_option' ), //becomes part of id attribute of error message
            __( 'Number must be a positive integer', 'wordpress' ), //default text zone
            'error'
        );
        $input = get_option( 'my_option' ); //keep old value
    }

    return $input;
}

Basically, if validation fails, you return the old saved value. Your validation is completely constraining the input values so no need to sanitize as well.

I don’t know how you’d hold the incorrect value in the form field if validation failed. As far as I can see, the settings API doesn’t allow for it directly. Someone else may be able to provide a solution?

tech