1.0.1 • Published 5 years ago

the-validation v1.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

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

IDNameRuleStatus
01isEmpty(str)requiredOK
02isAscii(str)asciiOK
03isUTF8(str)utf8OK
04isURL(str)urlOK
05isNumber(num)number:min:maxOK
06isEmail(str)emailOK
07isValidPassword(pwd[,useWeak])password:trueOK
08isRetype(str1, str2)retype:nameOK
09isMatch(value, pattern)regex:patternOK
10isValidDate(str)date:form:toOK
11isValidTime(str)timeOK
12isValidMonth(str)monthOK
13isValidWeek(str)weekOK
14isValidFile(file[, ext, size])file:extensions:maxsizeOK
15isPhoneNumber(str)phoneOK
16isMACAddress(addr)macOK
17isIPv4(addr)ipv4OK
18isIPv6(addr)ipv6OK
19isColor(str)colorOK
20isHash(str)hash:typeOK
21isJSON(str)jsonOK
22isUUID(str)uuidOK
23isCreditCard(str)credit:typeOK
24isBase32()UC
25isBase64()UC
26isJWT()UC
27isMongoId()UC
28isISIN()UC
29isISRC()UC
30isISBN()UC
31isISSN()UC
32isFQDN()EXP
33isBIC()EXP
34isPostalCode()UC
35isISO8601()UC
36isRFC3339()UC
37isDataURI()UC
38isMagnetURI()UC
39isMimeType()UC
40isLatLong()UC