I am trying to validate strings in ruby. Any string which contains spaces,under scores or any special char should fail validation. The valid string should contain only chars a-z
def alpha_numeric?(char)
if((char =~ /[[:alpha:]]) || (char =~ [[:digits:]]))
true
else
false
end
end
OR
def alpha_numeric?(char)
if(char =~ /[[:alnum:]])
true
else
false
end
end
We are using regular expressions that match letters & digits:
The above [[:alpha:]] ,[[:digit:]] and [[:alnum:]] are POSIX bracket expressions, and they have the advantage of matching unicode characters in their category.Hope this helps helps.
checkout the link below for more options: Ruby: How to find out if a character is a letter or a digit?