mercredi 21 octobre 2015

Regex to check alphanumeric string in ruby

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?

Aucun commentaire:

Enregistrer un commentaire