allow only alphnumeric entry and an underscore

I am taking an user input which should only be an alphanumeric character or an underscore. How to i do it?

man regex.

you can use character class for that purpose.

[a-z0-9_]

or in general,

[_[:alnum:]]
http://www.unix.com/shell-programming-scripting/44098-ksh-test-if-string-contains-alphanumeric.html

even this u can try :

echo 's$' | awk '$0 ~ /[^a-zA-Z0-9]/ { print "fail" }'