Reputation: 73
How can I prevent user uppercase letters input with
user = get.chomp
if I don`t want use
user.downcase!
I want user can`t input uppercase letters
Upvotes: 0
Views: 55
Reputation: 42182
Yes you can do that in the following way, getch takes one character at a time. One caviat: you need to end the string with two returns. Keep in mind that this is only the console, if you want more or fancier control use a simple GUI framework like eg Shoes of use a web-interface.
require 'io/console'
input, user = "", ""
while input != "\n"
input = STDIN.getch
if ('a'..'z').include? input
print input
user += input
end
end
puts "\nuser = #{user}"
Upvotes: 1
Reputation: 211560
If you want to verify that it's all downcase the easy way is:
user = gets.chomp
if (user == user.downcase)
# All downcase
end
You can also use a regular expression:
user = gets.chomp
if (user.match(/\A[a-z]\z/)
# All downcase
end
That will let you customize which characters are allowed, like if _
is acceptable.
You can't prevent them from typing in upper-case letters unless you do a lot of work with something like Curses to process individual keystrokes and assume control over echoing output. This is a ton of work and I wouldn't recommend it.
Upvotes: 0