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-zA-Z0-9 My code looks like.
def validate(string)
regex ="/[^a-zA-Z0-9]$/
if(string =~ regex)
return "true"
else
return "false"
end
I am getting error: TypeError: type mismatch: String given.
Can anyone please let me know what is the correct way of doing this?
^and$don't mean what you think they do in Ruby regexes, you almost always want\Aand\zinstead.\Aand\zit is then.\walso matches_(underscore/low line).