I need to not allow more than one period (.) in the string input. What is the best way to do it using Ruby regex?
Answering to comments:
I want to limit an input of only one period in email address during email registration. I have basic email regex for email input. My wonder is , if it’s possible to limit number of (.) through the Regex itself, or I should use Ruby libraries.
Yes, it is a newbie question.
Regular expression is as such
/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
How to only allow one period inside of any part of it?
Assuming you mean at most one period in the part before the @, theoretically it would be possible to do this with the following regex (it does not allow a dot as first or last character, and does not do any other validation):
The above regex only handles the part before the @, you’ll have to add the rest of the regex yourself. It basically matches a group of 1 or more non-period character, followed by an optional group of a period and followed by 1 or more non-period characters.
I’m however curious why you would want to limit registration to users with at most one period in their e-mail address… this does not seem like a sensible restriction, as it is quite realistic for users to legitimately have more than 1 period in their address.