Validators

Dozens of reusable validators for server, client or both.

Equal

Returns true if the test value is equal to the compare value.

Constructor options

Name type default description
compareValuenumeric | string | date object0The value to compare the test value with.

require(['mystique/Equal'], function(Equal){
    var validator = new Equal({'compareValue': myCompareValue}),
        result = validator.isValid(myValue);
})

var validator = new mystique.Equal({'compareValue': myCompareValue}),
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\Equal(['compareValue' => $myCompareValue]);
$result = $validator->isValid($myValue);

Greater Than

Returns true if the test value is greater than the compare value.

Constructor options

Name type default description
compareValuenumeric | string | date object0The value to compare the test value with.

require(['mystique/GreaterThan'], function(GreaterThan){
    var validator = new GreaterThan({'compareValue': myCompareValue}),
        result = validator.isValid(myValue);
})

var validator = new mystique.GreaterThan({'compareValue': myCompareValue}),
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\GreaterThan(['compareValue' => $myCompareValue]);
$result = $validator->isValid($myValue);

Greater Than or Equal

Returns true if the test value is greater than or equal to the compare value.

Constructor options

Name type default description
compareValuenumeric | string | date object0The value to compare the test value with.

require(['mystique/GreaterThanEqual'], function(GreaterThanEqual){
    var validator = new GreaterThanEqual({'compareValue': myCompareValue}),
        result = validator.isValid(myValue);
})

var validator = new mystique.GreaterThanEqual({'compareValue': myCompareValue}),
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\GreaterThanEqual(['compareValue' => $myCompareValue]);
$result = $validator->isValid($myValue);

Less Than

Returns true if the test value is less than the compare value.

Constructor options

Name type default description
compareValuenumeric | string | date object0The value to compare the test value with.

require(['mystique/LessThan'], function(LessThan){
    var validator = new LessThan({'compareValue': myCompareValue}),
        result = validator.isValid(myValue);
})

var validator = new mystique.LessThan({'compareValue': myCompareValue}),
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\LessThan(['compareValue' => $myCompareValue]);
$result = $validator->isValid($myValue);

Less Than or Equal

Returns true if the test value is less than or equal to the compare value.

Constructor options

Name type default description
compareValuenumeric | string | date object0The value to compare the test value with.

require(['mystique/LessThanEqual'], function(LessThanEqual){
    var validator = new LessThanEqual({'compareValue': myCompareValue}),
        result = validator.isValid(myValue);
})

var validator = new mystique.LessThanEqual({'compareValue': myCompareValue}),
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\LessThanEqual(['compareValue' => $myCompareValue]);
$result = $validator->isValid($myValue);

Not Equal

Returns true if the test value is not equal to the compare value.

Constructor options

Name type default description
compareValuenumeric | string | date object0The value to compare the test value with.

require(['mystique/NotEqual'], function(NotEqual){
    var validator = new NotEqual({'compareValue': myCompareValue}),
        result = validator.isValid(myValue);
})

var validator = new mystique.NotEqual({'compareValue': myCompareValue}),
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\NotEqual(['compareValue' => $myCompareValue]);
$result = $validator->isValid($myValue);

Credit Card

Returns true if the test value appears to be a valid credit card number.


require(['mystique/CreditCard'], function(CreditCard){
    var validator = new CreditCard,
        result = validator.isValid(myValue);
});

var validator = new mystique.CreditCard,
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\CreditCard;
$result = $validator->isValid($myValue);

Credit Card Expiry

Tests that the supplied value object is a valid two digit month and two digit year.

The value must be an object the form {month: int, year: int}


require(['mystique/CreditCardExpiry'], function(CreditCardExpiry){
    var validator = new CreditCardExpiry,
        result = validator.isValid(myValue);
});

The value must be an object the form {month: int, year: int}


var validator = new mystique.CreditCardExpiry,
    result = validator.isValid(myValue);

The value must be an associative array with the form ["month" => int, "year" => int]

$validator = new Zoop\Mystique\CreditCardExpiry;
$result = $validator->isValid($myValue);

Cvv

Returns true if the test value appears to be a valid credit card Cvv.


require(['mystique/Cvv'], function(Cvv){
    var validator = new Cvv,
        result = validator.isValid(myValue);
});

var validator = new mystique.Cvv,
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\Cvv;
$result = $validator->isValid($myValue);

Chain

Allows validators to be chained together.

Constructor options

Name type default description
validatorsarray[]An array of validator instances that will be evaluated in order. If all pass, then the Result value will be true. If one or more fail, then the Result value will be false. The order of evaluation can be truncated or altered with the haltOnPass haltOnFail skipOnPass skipOnFail properties of each validator. For more information see the spec.

require(['mystique/Alpha', 'mystique/Length', 'mystique/Chain'], function(Alpha, Length, Chain){
    var validator = new Chain({'validators': [
            new Alpha,
            new Length({'min': 5, 'max': 10})
        ]}),
        result = validator.isValid(myValue);
})

var validator = new mystique.Chain({'validators': [
        new mystique.Alpha,
        new mystique.Length({'min': 5, 'max': 10})
    ]}),
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\Chain(['validators' => [
    new Zoop\Mystique\Alpha,
    new Zoop\Mystique\Length('min' => 5, 'max' => 10)
]]);
$result = $validator->isValid($myValue);

Required

Place this validator at the start of a chain. If the test value is nullish, it will immediately return false without evaluating subsiquent validators in the chain.


require(['mystique/Required', 'mystique/Alpha', 'mystique/Chain'], function(Required, Alpha, Chain){
    var validator = new Chain({'validators': [
            new Required,
            new Alpha
        ]}),
        result = validator.isValid(myValue);
})

var validator = new mystique.Chain({'validators': [
        new mystique.Required,
        new mystique.Alpha
    ]}),
    result = validator.isValid(myValue);
})
$validator = new Zoop\Mystique\Chain(['validators' => [
    new Zoop\Mystique\Required,
    new Zoop\Mystique\Alpha
]);
$result = $validator->isValid($myValue);

Not Required

Place this validator at the start of a chain. If the test value is nullish, it will immediately return true without evaluating subsiquent validators in the chain.


require(['mystique/NotRequired', 'mystique/Alpha', 'mystique/Chain'], function(NotRequired, Alpha, Chain){
    var validator = new Chain({'validators': [
            new NotRequired,
            new Alpha
        ]}),
        result = validator.isValid(myValue);
})

var validator = new mystique.Chain({'validators': [
        new mystique.NotRequired,
        new mystique.Alpha
    ]}),
    result = validator.isValid(myValue);
})
$validator = new Zoop\Mystique\Chain(['validators' => [
    new Zoop\Mystique\NotRequired,
    new Zoop\Mystique\Alpha
]);
$result = $validator->isValid($myValue);

Boolean

Returns true if the test value is a boolean.


require(['mystique/Boolean'], function(Boolean){
    var validator = new Boolean,
        result = validator.isValid(myValue);
});

var validator = new mystique.Boolean,
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\Boolean;
$result = $validator->isValid($myValue);

Float

Returns true if the test value is numeric.


require(['mystique/Float'], function(Float){
    var validator = new Float,
        result = validator.isValid(myValue);
});

var validator = new mystique.Float,
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\Float;
$result = $validator->isValid($myValue);

Int

Returns true if the test value is an integer, or can be parsed to an integer.


require(['mystique/Int'], function(Int){
    var validator = new Int,
        result = validator.isValid(myValue);
});

var validator = new mystique.Int,
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\Int;
$result = $validator->isValid($myValue);

Date

Returns true if the test value appears is a Date object.


require(['mystique/Date'], function(Date){
    var validator = new Date,
        result = validator.isValid(myValue);
});

var validator = new mystique.Date,
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\Date;
$result = $validator->isValid($myValue);

String

Returns true if the test value is a string.


require(['mystique/String'], function(String){
    var validator = new String,
        result = validator.isValid(myValue);
});

var validator = new mystique.String,
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\String;
$result = $validator->isValid($myValue);

Alpha

Returns true if the test value contains only alpha characters.


require(['mystique/Alpha'], function(Alpha){
    var validator = new Alpha,
        result = validator.isValid(myValue);
});

var validator = new mystique.Alpha,
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\Alpha;
$result = $validator->isValid($myValue);

Email

Returns true if the test value appears to be a valid email.


require(['mystique/Email'], function(Email){
    var validator = new Email,
        result = validator.isValid(myValue);
});

var validator = new mystique.Email,
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\Email;
$result = $validator->isValid($myValue);

Hex Color

Returns true if the test value is a valid hexadecimal color code prefixed with #.


require(['mystique/HexColor'], function(HexColor){
    var validator = new HexColor,
        result = validator.isValid(myValue);
});

var validator = new mystique.HexColor,
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\HexColor;
$result = $validator->isValid($myValue);

Length

Returns true if the test value meets the specified length conditions.

Constructor options

Name type default description
minint0The minimum length.
maxint255The maximum length.

require(['mystique/Length'], function(Length){
    var validator = new Length({min: 10, max: 20}),
        result = validator.isValid(myValue);
})

var validator = new mystique.Length({min: 10, max: 20}),
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\Length(['min' => 10, 'max' => 20]) ;
$result = $validator->isValid($myValue);

Regex

Returns true if the test value matches the supplied regular expression.

Constructor options

Name type default description
regexstring$^The regular expression to test against.

require(['mystique/Regex'], function(Regex){
    var validator = new Regex({regex: '^[a-zA-Z]+$'}),
        result = validator.isValid(myValue);
})

var validator = new mystique.Regex({regex: '^[a-zA-Z]+$'}),
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\Regex;
$result = $validator->isValid(['regex' => '^[a-zA-Z]+$']);

Password

Returns true if the test value is likely to be a strong password. Length must be between 6 and 40 characters. Must contain at least one alpha and one numeric character.


require(['mystique/Password'], function(Password){
    var validator = new Password,
        result = validator.isValid(myValue);
});

var validator = new mystique.Password,
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\Password;
$result = $validator->isValid($myValue);

Slug

Returns true if the test value is a valid slug, that is, contains only the characters a-z, 0-9 and -, and meets the specified length conditions.

Constructor options

Name type default description
minint3The minimum length of the slug.
maxint255The maximum length of the slug.

require(['mystique/Slug'], function(Slug){
    var validator = new Slug,
        result = validator.isValid(myValue);
})

var validator = new mystique.Slug,
    result = validator.isValid(myValue);
$validator = new Zoop\Mystique\Slug;
$result = $validator->isValid($myValue);