I have defined 2 regular expressions.
ip_regex = /^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/
hostname_regex = /^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$/
I need to allow users to input either an IP address or a Hostname in to a textfield. My validation code is as follows.
validates :ip_address, :presence => true,
:format => { :with => ip_regex || hostname_regex }
I've verified that the ip_regex is successful. However, the hostname_regex isn't. It appears that either the hostname_regex or my validation code is flawed. Which is it? How do I resolve this issue? Is there a more effective &/or efficient way of performing this task?