you are here: codestackercodes [RSS] → tag validation [RSS]

rails 3 validates method

validates :login, :presence => true, :length => {:minimum => 4}, :uniqueness => true, :format => { :with => /[A-Za-z0-9]+/ }

# :presence => true

# :uniqueness => true

# :numericality => true

# :length => { :minimum => 0, maximum => 2000 }

# :format => { :with => /.*/ }

# :format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i } # email format

# :format => { :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix } # url format

# :inclusion => { :in => [1,2,3] }

# :exclusion => { :in => [1,2,3] }

# :acceptance => true

# :confirmation => true

rails 3 money validation

validates :price, :presence => true, :format => { :with => /[0-9\,]*/ }

email regex

validates_format_of :email, 
: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]$/, 
:message => "is invalid" 

validation client-side with form_tag and form_remote_tag

create your javascript validate function and call in your form
<% form_tag "/login", :onsubmit => 'return validate(this)' do %>
    <!-- your form -->
<% end %>

<!-- or -->

<% form_remote_tag :url => "/login", :update => 'temp', :before => 'if( !validate(this) ) return false' do %>
    <!-- your form -->
<% end %>

realtime email validation

requires prototype and script.aculo.us demo: http://ajaxorized.com/examples/scriptaculous/email.html
var isValid = false;

validateEmail = function(e) {
	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)) {
		if(!isValid) {
			$(e).morph('border-color:#00FF00', {duration:.3});
			isValid = true;
		}
	} else {
		if(isValid) {
			$(e).morph('border-color:#FF0000', {duration:.3});						
			isValid = false;					
		}
	}	
}

// <input type = "email" id = "myinput" onKeyUp = "validateEmail(this)" />

validation sample

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

validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :on => :create, :message=>"has an invalid format" 
validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => 'email must be valid'
validates_format_of :login, :with => /\w+@\w+\.\w{2}/

Displaying all 6 Code