email regex
1 validates_format_of :email, 2 :with => /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/, 3 :message => "is invalid"
1 validates_format_of :email, 2 :with => /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/, 3 :message => "is invalid"
create your javascript validate function and call in your form
1 <% form_tag "/login", :onsubmit => 'return validate(this)' do %> 2 <!-- your form --> 3 <% end %> 4 5 <!-- or --> 6 7 <% form_remote_tag :url => "/login", :update => 'temp', :before => 'if( !validate(this) ) return false' do %> 8 <!-- your form --> 9 <% end %>
requires prototype and script.aculo.us
demo: http://ajaxorized.com/examples/scriptaculous/email.html
1 var isValid = false; 2 3 validateEmail = function(e) { 4 if(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/.test(e.value)) { 5 if(!isValid) { 6 $(e).morph('border-color:#00FF00', {duration:.3}); 7 isValid = true; 8 } 9 } else { 10 if(isValid) { 11 $(e).morph('border-color:#FF0000', {duration:.3}); 12 isValid = false; 13 } 14 } 15 } 16 17 // <input type = "email" id = "myinput" onKeyUp = "validateEmail(this)" />
1 validates_format_of :company_url, :with => /((http|https):\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?)/ # valida URL 2 validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :on => :create, :message=>"has an invalid format" 3 validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => 'email must be valid' 4 validates_format_of :login, :with => /\w+@\w+\.\w{2}/ 5