1.0.1 • Published 5 years ago
the-validation v1.0.1
the-validation
What is this?
A pure Javascript plugin ( Extended from validator by cohara87 ) used to validate all HTML Input Types.
It can also be used to validate data independently.
Currently, no XSS sanitizer is supported.
Form handling
Client side:
<form name="the_form">
<input type="text" name="the_text">
<input type="number" name="the_number">
<button>Submit</button>
</form>
<script src="the-validation.js"></script>
<script>
const validator = new TheValidation( {
'the_text': 'required|email',
'the_number': 'required|number'
}, "the_form" );
validator.submit( function( res, err ) { // onsubmit
if( !res ) {
console.log( "Error Message", err );
}
} );
</script>
Server side:
const TheValidation = require( 'the-validation' );
// OR
import TheValidation from 'the-validation';
// ▼------------ Go------------- ▼
var data = { "the_text": "example@gmail.com" }
const validator = new TheValidation( {
'the_text': 'required|email'
}, data );
if( !validator.validate() ) {
console.log( "Error message", validator.errorMessage );
}
Single data handling
Client side:
<script src="the-validation.js"></script>
<script>
var validator = new TheValidation();
console.log( validator.isNumber( 10 ) ); // true
</script>
Server side:
const TheValidation = require( 'the-validation' );
const validator = new TheValidation();
console.log( validator.isNumber( 10 ) ); // true
Set Error Messages with setMessage(rule, msg)
:
const TheValidation = require( 'the-validation' );
const validator = new TheValidation( {
'data': 'required|number:10:1000'
}, "the_form" );
validator.setMessage( "required", "Please fill out this field." );
// With format based on all rules below
validator.setMessage( "number", "Accept only numbers within a range of #min# and #max#." );
Validate functions
ID | Name | Rule | Status |
---|---|---|---|
01 | isEmpty(str) | required | OK |
02 | isAscii(str) | ascii | OK |
03 | isUTF8(str) | utf8 | OK |
04 | isURL(str) | url | OK |
05 | isNumber(num) | number:min:max | OK |
06 | isEmail(str) | OK | |
07 | isValidPassword(pwd[,useWeak]) | password:true | OK |
08 | isRetype(str1, str2) | retype:name | OK |
09 | isMatch(value, pattern) | regex:pattern | OK |
10 | isValidDate(str) | date:form:to | OK |
11 | isValidTime(str) | time | OK |
12 | isValidMonth(str) | month | OK |
13 | isValidWeek(str) | week | OK |
14 | isValidFile(file[, ext, size]) | file:extensions:maxsize | OK |
15 | isPhoneNumber(str) | phone | OK |
16 | isMACAddress(addr) | mac | OK |
17 | isIPv4(addr) | ipv4 | OK |
18 | isIPv6(addr) | ipv6 | OK |
19 | isColor(str) | color | OK |
20 | isHash(str) | hash:type | OK |
21 | isJSON(str) | json | OK |
22 | isUUID(str) | uuid | OK |
23 | isCreditCard(str) | credit:type | OK |
24 | isBase32() | UC | |
25 | isBase64() | UC | |
26 | isJWT() | UC | |
27 | isMongoId() | UC | |
28 | isISIN() | UC | |
29 | isISRC() | UC | |
30 | isISBN() | UC | |
31 | isISSN() | UC | |
32 | isFQDN() | EXP | |
33 | isBIC() | EXP | |
34 | isPostalCode() | UC | |
35 | isISO8601() | UC | |
36 | isRFC3339() | UC | |
37 | isDataURI() | UC | |
38 | isMagnetURI() | UC | |
39 | isMimeType() | UC | |
40 | isLatLong() | UC |