$mform->addRule('name', null, 'required', null, 'client');
$mform->addRule('fieldname', 'sometext', 'regex', '^[^-]+$', 'server');
$mform->addRule('name', 'Field should contain only characters and numbers.', 'regex', '/^[^_ ]+$/', 'server');
But still it accepts the special chars.
Am I doing any mistake?
As Darko said, "Add any other characters you do not wish to have in the text". The regex as written here will allow any character that's not an underscore. If you need to restrict other characters, you'll need to add them to the excluded class.
In general, if you are going to include regular expressions in your code, I would highly reccommend getting a good understanding of how they work and what they mean, not just copy/pasting blindly, as they are very powerful and easy to get wrong. Regexr is a helpful tool for explaining and testing expressions.