gqli
gqli

Reputation: 1045

How to validate user's input for English alphabet only in Rails?

Im experimenting on a rails app . I would like to Validate user's input for English alphabet characters only in rails. Let's say we have a sign up form for user fill in their: Name, Address, Email address. But i would like them to fill the Address form with English alphabet only, Because some of them might accidentally fill in with Chinese alphabet which is not what we want.

Can we sort this out by a regular expression ? if so please guide me to write those three cases : (Any other ways are very welcome)

1: validate "English alphabet only "

2: Validate "Chinese alphabet is not allowed "

Your help will be very much appreciated !

Upvotes: 0

Views: 3550

Answers (2)

Alex Pan
Alex Pan

Reputation: 4571

Use validates_format_of:

validates_format_of :address, :with => /^[A-Za-z0-9 ]*$/

Upvotes: 5

potashin
potashin

Reputation: 44581

You can use validates with format:

validates :address, format: { with: /[a-zA-Z0-9]/}

Upvotes: 5

Related Questions