_v_.js

Born to validate stuff in much more easy way than you can imagine.

Download production || development version.

Independent,
extendable,
powerful,
intuitive,
independent,
easy to use...

Need more info? -> read api's and play with demos.

Want more functionality? -> lets discuss it!



Originally developed for tFormer.js by Vasiliy Zubach (aka @TjRus).

Developed with insane hate to validation and currently existing validators. Also with MBPR, PHPStorm, Chrome and brain (a little) o_O.

Documentation:




Install & Usage:


  • Download:

    Production version (minified) or Developers version or clone them from Github repository:

    git clone https://github.com/TjRus/_v_.js.git

  • Add to your page:

    Or include the source to your js script. (it's MIT)

    <script src="_v_.min.js"></script>

  • Use for validating stuff:
    // short way
    _v_('string you want to validate').validate('rule'); // => {boolean}
    _v_('str').validate('* l>2'); // => true
    // longer way
    _v_('str').rules('* l=3').validate(); // => true
    _v_('str').rules('@').validate(); // => false
    


Methods:


separate( str )

Setting rules separator string.

  • str {string} - simple character or character combination for separating rules string.
    By default - ' ' (space symbol)

Return: original _v_ object;

rules( str )

Setting validator rules.

  • str {string} - simple character or character combination for separating rules string.
    By default - ' ' (space symbol)

Return: original _v_ object;

addRule( str )

Add new rules to already defined rules string.

  • str {string} - new rules string that should be added.

Return: original _v_ object;

delRule( str )

Delete some rules from already defined rules string.

  • str {string} - rules string that should be deleted.

Return: original _v_ object;

hasRule( str )

Check is current rules string has some rules.

  • str {string} - rules that we should check.

Return: {boolean}

parseRules( )

Parse rules string and its parameters.

Return: {object} with `key` - rule, `value` - rule values/parameters;

validate( rules )

Run validator and check is your test string is valid.

Return: {boolean};

extend( rule_key, func )

Extend _v_ object with new validation rule and function.

  • rule_key {string} - new rule key. Like ('=', 'a1_', 'ip', etc...).
  • func {function} - validation function with original _v_ object as context (this).
    If rule_key has its parameters (rule_key[parameters,array]) they are passed as attribute to func

Examples: extend demo 1, extend demo 2.


Rules:



Rules can be combined and divided by separator symbol (by default it's ' ' - empty space). Example: `* @ l>10`
Also you can extend _v_ with your own rules and validation functions.

* - required. Check is tested value not empty.

reg= - Regular. Check test value with regular expression. Examples: reg=^([a-z]{2,4})(((\\.)([A-Za-z0-9-]+))+)$, etc..

a - alpha. Check is tested value consists of alphabet symbols.

a1 - alpha numeric. Check is tested value consists of alphabet and numeric symbols.

a_ - alpha dash. Check is tested value consists of alphabet symbols and dash.

a1_ - alpha numeric dash. Check is tested value consists of alphabet, numeric symbols and dash.

@ - email. Check is tested value a valid email.

@s - emails. Check is tested value a valid email or emails separated by coma (',').

ip - IP. Check is tested value a valid IP address.

url - URL. Check is tested value a valid URL.

b64 - Base64. Check is tested value a valid Base64 string.

int - Integer. Check is tested value Integer.

dec - Decimal. Check is tested value Decimal.

nat - Natural. Check is tested value Natural.

num - Numeric. Check is tested value Numeric.

Length compare:

l= - Length equals to some value. Examples: l=10, l=5, etc..

l> - Length more than some value. Examples: l>10, l>5, etc..

l>= - Length more or equals to some value. Examples: l>=10, l>=5, etc..

l< - Length less than some value. Examples: l<10, l<5, etc..

l<= - Length less or equals to some value. Examples: l<=10, l<=5, etc..

lr= - Length range. Check that current test value length is in range. Examples: lr=[10,15], lr=[0,5], etc..

Numbers compare:

> - Check is tested value more than some other value. Examples: >10, >5, etc..

>= - Check is tested value more or equals to some other value. Examples: >=10, >=5, etc..

< - Check is tested value less than some other value. Examples: <10, <5, etc..

<= - Check is tested value less or equals to some other value. Examples: <=10, <=5, etc..

r= - Range. Check that tested value is in range. Examples: r=[10,15], r=[0,5], etc..

Compare matches:

= - matches to. Check that tested value matches to other value. Examples: =10 (matches '10'), =[0,5] (matches '0' or '5'), etc..

= - matches to. Check that tested value matches to other value. Examples: =10 (matches '10'), =[0,5] (matches '0' or '5'), etc..

=# - matches to ID. Check that tested value matches to value of element with specified ID. Examples: =#test_id (matches to 'test_id' value), =#[test_id_1,test_id_2] (matches 'test_id_1' or 'test_id_2' value), etc..

=[10,#some_id] - will check is tested value matches to '10' or 'some_id' value.

!= - not matches to. Check that tested value not matches to other value. Examples: !=10 (not matches '10'), !=[0,5] (not matches '0' and '5'), etc..

! - not contain. Check that tested value not contain defined value. Examples: !=.ua (not contain '.ua'), !=[5,.ua] (not contain '5' and '.ua'), etc..

Credit cards:

c - Card. Check credit cards of all types.

cv - Visa Card

cm - Master Card

ca - American Express Card

cd - Discover Card

Date:

D= - Date. Validate test value in differend date formats. All available format keys are in the table below.

Field Full Form Short Form
Year Y (4 digits) y (2 digits)
Month M (1 or 2 digits) m (1 or 2 digits)
Month Name N (full name) n (abbr)
Day of Month D (1 or 2 digits) d (1 or 2 digits)
Day Name W (full name) w (abbr)
Hour (1-12) H (1 or 2 digits) h (1 or 2 digits)
Hour (0-23) R (1 or 2 digits) r (1 or 2 digits)
Minute I (1 or 2 digits) i (1 or 2 digits)
Second S (1 or 2 digits) s (1 or 2 digits)
AM/PM A (upper case) a (lower case)

Example: D=Y-M-D (like 1989-08-04), D=Y-M-D R:I:S (like 1989-08-04 20:12:43), etc..

In this demos you can edit some method parameters

Full usage demo:

// standard defining
var test1 = _v_('i+test@tjrus.com').separate(' ').rules('* @ ![10,11,12]');

// rules modifications
test1.addRule('l>10'); // => rules after adding: 
test1.delRule('* !10'); // => rules after deleting: 

// checking
test1.hasRule('@ !11'); // => 
test1.validate() // => 

// summarized test: _v_ object
test1 = _v_ {
	parsedRules:  ,
	value: ' ',
	rule: ' ',
	separator: ' '
};

All in one line demo:

// fullstack defining
var test2 = _v_('i+test@tjrus.com').separate(' ').rules('* @ ![10,11,12]').addRule('').delRule('');
// checking
test2.hasRule('*'); // => 
test2.validate() // => 

Short demo:

// short validation check
_v_('i+test@tjrus.com').validate('@'); // => 

Extend demo:

// lets extend _v_ with new rule and validation function
_v_().extend('ip||url', function() {
	var test = this.value;
	return _v_(test).validate('ip') || _v_(test).validate('url');
} );
_v_('i+test@tjrus.com').validate('ip||url'); // => 
_v_('http://tjrus.com').validate('ip||url'); // => 

Extend demo2 (with value in function):

// lets extend _v_ with new rule and validation function
_v_().extend('%', function(num) {
	return this.value % num == 0;
} );
_v_('10').rules('%5').validate(); // => 
comments powered by Disqus