The regex need delimiters, try with '/^[^_ ]+$/'
$mform->addRule('name', 'Field should contain only characters and numbers.', 'regex', '/^[^_ ]+$/', 'server');
$mform->addRule('name', 'Field should contain only characters and numbers.', 'regex', '/^[^_ ]+$/', 'server');