I finally figured this out. The Custom Meta Boxes and Fields plugin has a sanitize setting where you can add your own sanitize function or use one of the default wordpress sanitize functions. In my case, Wordpress already has a sanitize function that does exactly what I wanted. See the code below, specifically the line
"'sanitization_cb' => 'sanitize_html_class',"
array(
'name' => __( 'Custom Body Class', 'cmb' ),
'id' => $prefix . 'wnd_bodyclass',
'type' => 'text_medium',
'sanitization_cb' => 'sanitize_html_class', // custom sanitization callback. see this page for details: https://codex.wordpress.org/Function_Reference/sanitize_text_field
),
array(
'name' => __( 'Custom Post Class', 'cmb' ),
'id' => $prefix . 'wnd_postclass',
'type' => 'text_medium',
),
ctype_alnum
php.net/manual/en/function.ctype-alnum.php. It will check for only alpha numeric; and returntrue
orfalse
.ctype_alnum
function.if(ctype_alnum($data)===true) {echo 'success'} else {echo 'try again'}