jsbeans

jsbeans  1.0.0

jsbeans > jsbeans > jsbeans.Validator
Search:
 
Filters

static Class jsbeans.Validator

A form's validator

Properties

charCount - static object

Validator extensions for counting characters in inputs.

charCount.EXCLUDES - static Array

List of characters excluded from counting. By defalut it has just the newline char, the most common char for textareas (according to developers).
It may be modified according to yours needs (just remember it's static) or use the optional excludes parameter invoking the validation method.

charCount.messages - static JSON

Default messages for Validator.charCount extension

errors - static Array<JSON>

An Array of errors. Each error is a JSON in the form.
object: <DOM>, // the invalid input DOM Object
label: <String>, // the label for object as returned by getLabelFor
type: <String>, // a string representing the name of the validation ('required', 'maxLen', ...)
message: <String> // the string containing the error message

file - static object

Validator extensions for file extensions.
Sample usage: file_input: ["file=pdf,txt"]

file.messages - static JSON

Default messages for Validator.file extension

fiscalCode - static object

Validator extensions for fiscal code.

fiscalCode.messages - static JSON

Default messages for Validator.fiscalCode extension

iban_it - static object

Validator extensions for IBAN (International Bank Account Number) for italian bank accounts (BBAN of 23 characters).
Definition is here

iban_it.messages - static JSON

Default messages for Validator.iban_it extension

ip - static object

Validator extensions for IP addresses

ip.messages - static JSON

Default messages for Validator.ip extension

length - static object

Validator extensions for exact input's length

length.messages - static JSON

Default messages for Validator.length extension

messages - static JSON

Default messages for default asserts.

messages.it - static object

Italian labels for messages.

options - static JSON

Default options are:
errorClass: null, // the style class to apply in case of invalid value
// default function invoked in case of at least one invalid value.
// It get an <Array<JSON>> and this options as arguments
callback: function(errors, options) {
jsbeans.Validator.manageErrors(errors, options);
},
propagate: true, // if false validation will stop at first error
focusOnFirst: true, // if true focus will be applied in first invalid input
extraFunctions: null, // a list of functions for extra validation, useful for custom validation
locale: "en" // default locale

passwordStrength - static object

Validator extensions for password strength.
It tests for length (minimum 6 characters), case (both cases), numbers and characters mix, two sets of other characters (.+-_! and |$&()?'^[]{}#,;:)
Each of them has a weight:
length: 0.3
case: 0.25
mix: 0.25
a set of other chars: 0.1 each
---------------------------------
of course the sum is 1
A strong password has at least 0.6. This means the password has to check at least 3 of provided controls.

passwordStrength.messages - static JSON

Default messages for Validator.passwordStrength extension

regExp - static object

Validator extensions for Regular Expressions

regExp.messages - static JSON

Default messages for Validator.regExp extension

sameAs - static object

Validator extensions for comparing two input's values

sameAs.messages - static JSON

Default messages for Validator.sameAs extension

time - static object

Validator extensions for time

time.messages - static JSON

Default messages for Validator.time extension

url - static object

Validator extensions for uri (http,https,ftp,file, ...)

url.messages - static JSON

Default messages for Validator.url extension

wordCount - static object

Validator extensions for counting words in inputs.

wordCount.EXCLUDES - static Array

List of words and marks excluded from counting. It has punctation marks, brackets, arithmetics marks, ...
It may be modified according to yours needs (just remember it's static) or use the optional excludes parameter invoking the validation method. See here and here for a list of HTML/javascript entities.

wordCount.messages - static JSON

Default messages for Validator.wordCount extension

Methods

assertCharCount

static boolean assertCharCount ( input , params )
Checks if {@param} input's value contains not more than a given number of characters.
This method does NOT take into account some characters as defined by jsbeans.Validator.charCount.EXLUDES array.
{@param} params may be an just Integer to check the maximum mumber of characters or a JSON in the following form:
  • limit <Integer> (required): maximum number of characters aspected
  • excludes <Array>: if provided this array overrides jsbeans.Validator.charCount.EXCLUDES
{@param} excludes items are checked using the == (equals) operator.
Samples:
  1. ["charCount=10"]: returns false if {@param} input's value contains 11 or more characters
  2. ["charCount={limit:10,excludes:[]}"]: returns false if {@param} input's value contains 11 or more characters, none excluded.
Parameters:
input <DOM> input to check
params <Integer | JSON> limit for number of characters OR a JSON with limit (Integer, required) and excludes (Array, optional).
Returns: boolean
true if input's value has less than specified rules (see description).

assertDate

static boolean assertDate ( input , options )
Validation for Date with optional check for format, min date (after) and max date (before).
Parameters:
input <DOM> the input to validate
options <JSON>
format: <String>, // only dd/MM/yyyy and MM/dd/yyyy allowed, default dd/MM/yyyy if browser's language is "IT", MM/dd/yyyy otherwise
min: <String>, // a string representing a date in the same format of 'format' option, default 01/01/1970
max: <String>, // a string representing a date in the same format of 'format' option, default 01/01/3000
For min and max options you can use the string today as special value or the name of a global function returning the time (Date object or milliseconds) to check against.
Returns: boolean

assertEmail

static boolean assertEmail ( input )
Validation of email address
Parameters:
input <DOM> the input to validate
Returns: boolean
true for valid email address

assertFile

static boolean assertFile ( input , re )
Tests {@param} input's value against given {@param} re.
Parameters:
input <DOM> the input to validate
re <String> a Regular Expression
Returns: boolean
returns the result of re.test(input.value)

assertFiscalCode

static boolean assertFiscalCode ( obj , [forceStrict] )
Checks if {@param} obj's value is a valid fiscal code.
If {@param} forceStrict is set to true the validation id not only formal (digits and characters in the right place).
A valid format is ('D' for digits, 'C' for character): CCCCCCDDCDDCDDDC
Parameters:
obj <DOM> input to check
[forceStrict] <boolean> true to check fiscal code integrity, not only formally. Default false.
Returns: boolean
true if obj's value is a valid fiscal code.

assertIban_it

static boolean assertIban_it ( input , [controlDigitsCheck] )
Checks for a valid italian IBAN in both format:
IT 12 L 12345 12345 123456789012
and
IT12L1234512345123456789012
Parameters:
input <DOM> the input to validate
[controlDigitsCheck] <boolean> if true checks if control digits used in current iban are the same as the aspected ones (European CIN)
Returns: boolean
true if input's value is a valid italian IBAN

assertInteger

static boolean assertInteger ( input )
Validation for integers.
Parameters:
input <DOM> the input to validate
Returns: boolean
false if input's value isn't a number (as returned by javascript's native isNaN) and it contains dots or commas.

assertIp

static boolean assertIp ( input )
Checks if {@param} input's value contains a string representing a valid IPv4 address.
Note that this method uses javascript's native parseInt function to check each octect, so even 01.01.01.01 may result in a valid IP address.
Parameters:
input <DOM> the input to validate
Returns: boolean
true if input's value is valid.

assertLength

static boolean assertLength ( input , lengths )
Checks if {@param} input's length is as aspected.
Parameters:
input <DOM> the input to validate
lengths <Integer | Array[<Integer>]> may be just an Integer or an Array of Integers ([3,7,11]) if input's value may have more then one length (think about European CIN that can have 8 OR 11 characters)
Returns: boolean
true if input's length is as long as one of the passed length.

assertMaxLen

static boolean assertMaxLen ( input , maxLen )
Checks if {@param} input's value length is less or equals than {@param} maxLen.
Parameters:
input <DOM> the input to validate
maxLen <Integer | String> Both Integers or Strings may be used as they will be parsed by javascript's native parseInt function.
Returns: boolean

assertMaxValue

static boolean assertMaxValue ( input , maxValue )
Checks if {@param} input's value is less or equals than {@param} maxValue using javascript's native parseFloat function.
Parameters:
input <DOM> the input to validate
maxValue <Float>
Returns: boolean

assertMinLen

static boolean assertMinLen ( input , minLen )
Checks if {@param} input's value length is greater or equals than {@param} minLen.
Parameters:
input <DOM> the input to validate
minLen <Integer | String> Both Integers or Strings may be used as they will be parsed by javascript's native parseInt function.
Returns: boolean

assertMinValue

static boolean assertMinValue ( input , minValue )
Checks if {@param} input's value is greater or equals than {@param} minValue using javascript's native parseFloat function.
Parameters:
input <DOM> the input to validate
minValue <Float>
Returns: boolean

assertNumeric

static boolean assertNumeric ( input )
Validation for numeric, integers included, using javascript's native isNaN function.
Parameters:
input <DOM>
Returns: boolean

assertPasswordStrength

static boolean assertPasswordStrength ( input )
See {@param} jsbeans.Validator.passwordStrength for details.
Parameters:
input <DOM> the input to validate
Returns: boolean
true if at least 0.6 is reached as every control has a weight.

assertRequired

static boolean assertRequired ( input )
For mandatory fields.
Parameters:
input <DOM> the input to validate
Returns: boolean
false if input is empty (trimmed) and checkbox or radio are not checked

assertRequiredByName

static boolean assertRequiredByName ( input )
For mandatory fields with same 'name' attribute of the given one. Each field is checked using {@method} assertRequired method.
Useful for input of type radio and checkbox.
Parameters:
input <DOM> one of the input whose name is used to retrieve all the inputs to be validated
Returns: boolean
true if there is at least one valid field

assertSameAs

static boolean assertSameAs ( input , compare )
Checks if {@param} input has same value of {@param} compare
Parameters:
input <DOM> first input
compare <String> the id of a second input whose value will be compared to first one
Returns: boolean
true if input's value is equals to compare's using == operator.

assertTime

static boolean assertTime ( input , [timeFormat] )
Checks if {@param} input's value contains a string representing a time matching given {@param} timeFormat
Parameters:
input <DOM> the input to validate
[timeFormat] <String> one among "hh:mm", "hh.mm", "hh:mm:ss" or "hh.mm.ss", default "hh:mm"
Returns: boolean
true if input's value is valid for given timeFormat.

assertUrl

static boolean assertUrl ( input , [protocol_list] )
Checks if {@param} input's value contains a string representing an url, optionally matching one of given {@param} protocol_list
Parameters:
input <DOM> the input to validate
[protocol_list] <String> a CSV (Comma Separeted Values) of allowed protocols.
Note that, by now, validation checks against only the following protocols: file, gopher, news, nntp, telnet, http, ftp, https, ftps, sftp.
Returns: boolean
true if input's value matches one of given protocol_list

assertWordCount

static boolean assertWordCount ( input , params )
Checks if {@param} input's value contains not more than a given number of words.
This method does NOT take into account some characters as defined by jsbeans.Validator.wordCount.EXLUDES array.
It also try to intercept email and web (http|https) addresses counting them as one word (each).
Numbers are parsed as floats and always counted, regardless of their length as strings.
{@param} params may be an just Integer to check the maximum mumber of words or a JSON in the following form:
  • limit <Integer> (required): maximum number of words aspected
  • skip <Integer>: skips words shorter than {@param} skip. Default: no skip
  • excludes <Array>: if provided this array overrides jsbeans.Validator.wordCount.EXCLUDES
{@param} excludes items are checked using regular expression: new RegExp(ps.excludes[i], 'gi').
Samples:
  1. ["wordCount=10"]: returns false if {@param} input's value contains 11 or more words
  2. ["wordCount={limit:10,skip:3}"]: returns false if {@param} input's value contains 11 or more words longer than 3 characters
Parameters:
input <DOM> input to check
params <Integer | JSON> limit for number of words OR a JSON with limit (Integer, required), skip (Integer, optional) and excludes (Array, optional).
Returns: boolean
true if input's value has less than specified rules (see description).

charCount.getCount

static Integer charCount.getCount ( input , params )
Core counter. It takes same arguments (and it is used by) as {@method} assertCharCount.
It may be useful to display current number of characters. Beware of firing with onkeyup event.
Parameters:
input <DOM> input to check
params <Integer | JSON> limit for number of chars OR a JSON with limit (Integer, required), skip (Integer, optional) and excludes (Array, optional).
Returns: Integer
the current number of characters in input; default 0

getLabelFor

static String getLabelFor ( id )
A convenince method to get a human readble label for inputs. It always tries to return something in the following order:
  1. looks in the page for a label tag with the 'for' attribute set to {@param} id
  2. if there's no DOM Object with given id, the id itself is returned
  3. looks for the 'title' attribute of the DOM Object as returned by a document.getElementById call
  4. looks for the 'name' attribute of the DOM Object as returned by a document.getElementById call
    1. Result is trimmed to avoid unwanted characters such as spaces, newlines, ...
Parameters:
id <String> the DOM Object id
Returns: String

init

static void init ( options )
Use this method instead of overriding jsbeans.Validator.options one by one or passing options on each validate invocation.
Parameters:
options <JSON> same as options
Returns: void

manageErrors

static void manageErrors ( errors , options )
Default function delegated to print errors. It uses an alert.
Parameters:
errors <Array<JSON>> see jsbeans.Validator.errors for details.
options <JSON> see jsbeans.Validator.options for details.
Returns: void

reset

static void reset ( )
Brings jsbeans.Validator.options to defaults.
Returns: void

setLocale

static void setLocale ( locale )
Loads a locale and sets it for error messages.
Parameters:
locale <String> a string representing a locale. It loads <jsbeans_script_location>/Validator_<locale>.js.
Returns: void

validate

static boolean validate ( inputs , [options] )
Main method.
Sample:
<form action="an_action" onsubmit="return validate();">
<label for="firstname">First Name<label>: <input type="text" id="firstname" /><br/>
<label for="email">Email address<label>: <input type="text" id="email" /><br/>
<label for="age">First Name<label>: <input type="text" id="age" />
</form>
<script type="text/javascript">
function validate() {
return jsbeans.Validator.validate({
firstname: ["required"],
email: ["required", "email"],
age: ["minValue=18"]
});
}
</script>
Parameters:
inputs <JSON> a JSON with the id of the input to validate as key and an Array of validation's asserts as value.
[options] <JSON> override of default jsbeans.Validator.options
Returns: boolean
true if there are no errors

wordCount.getCount

static Integer wordCount.getCount ( input , params )
Core counter. It takes same arguments (and it is used by) as {@method} assertWordCount.
It may be useful to display current number of words. Beware of firing with onkeyup event.
Parameters:
input <DOM> input to check
params <Integer | JSON> limit for number of words OR a JSON with limit (Integer, required), skip (Integer, optional) and excludes (Array, optional).
Returns: Integer
the current number of words in input; default 0


Copyright © 2016 Francesco Mele. All rights reserved.