Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.
/ validate_me Public archive

[DEAD] jQuery form validation helpers. Mimics the validates methods of Rail

Notifications You must be signed in to change notification settings

daneharrigan/validate_me

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 

Repository files navigation

validate_me (in jQuery)

NOTE: This repository will be renamed to validate_me after the Rackspace move is complete.

I hate doing form validation. It’s that simple. So I figured I’d take a few extra minutes, er… days, ok a week or two and put together validate_me as a jQuery plugin.

I don’t mind writing validation logic with Ruby on Rails validates_* methods so I mimicked that style as best I could in JavaScript.

The Syntax

Below you’ll find code examples. There is also an example.html file to show this plugin in action.

validates_presence_of

Define one at a time:

$.validates_presence_of('first_name');
$.validates_presence_of('last_name');

Or define them in one line:

$.validates_presence_of('first_name','age');

validates_numericality_of

$.validates_numericality_of('age', {
	on: ['keyup','blur'], // a single event can be passed as a string (see next example)
	greater_than: 18,
	less_than: 35,
	greater_than_or_equal_to: 18,
	less_than_or_equal_to: 35,
	equal_to: 24,
	odd: true,
	even: true,
	message: 'Sorry the age was incorrect.' // this will overwrite the default failing message
});

Of course defining all of these requirements at once makes no sense, but now you can easily see what options you have available.

validates_length_of

$.validates_length_of('first_name',{
	on: 'keyup',
	minimum: 2,
	maximum: 50,
	is: 4,
	within: [1,50],
	message: 'Sorry the first name was incorrect.' // this will overwrite the default failing message
});

Once again, all the available options are presented in this example, but defining them all wouldn’t be too sweet.

validates_format_of

$.validates_format_of('middle_initial',{
	on: 'keyup',
	with: /^[A-Z]$/,
	message: 'Sorry the middle initial was incorrect.' // this will overwrite the default failing message
});

This would probably be used to validate an email address or possibly a phone number. My example is simply validating a single capitalized character.

validates_exclusion_of

$.validates_exclusion_of('username',{
	on: 'blur',
	in: ['admin','root','superuser'],
	message: 'Sorry that is an incorrect username.' // this will overwrite the default failing message
});

This is a very simple method for defining what values a field cannot be. It wouldn’t be smart to allow someone to create a username of root or admin.

validates_inclusion_of

$.validates_inclusion_of('language',{
	on: 'blur',
	in: ['ruby','php','perl','python','java','c','c++'],
	message: 'Sorry we were not asking for that language.' // this will overwrite the default failing message
});

An inverse of validates_exclusion_of, this method allows you to define the only accepted values from a field.

validates_confirmation_of

Coming soon…

validates_uniqueness_of

Coming soon…

Defaults/Customization

By default when a field fails the validation an error class is applied to it. Once the field passes the class is removed. Custom failing presentation can be defined like so:

$.validate_highlighter({
	fail: function(element){
		$(element).parent().addClass('error');
	},
	pass: function(element){
		$(element).parent().removeClass('error');		
	}
});

Notice element is passed into the function. Now the parent element of the field is given the error class.

Incomplete

  • validates_uniqueness_of
  • validates_confirmation_of
  • compressed version

About

[DEAD] jQuery form validation helpers. Mimics the validates methods of Rail

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published