sfv

string-format-validation

Javascript library to format & validate strings (or Numbers)

Showing:

Popularity

Downloads/wk

23

GitHub Stars

1

Maintenance

Last Commit

5yrs ago

Contributors

1

Package

Dependencies

2

Size (min+gzip)

8.9KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Readme

npm version semantic-release

Build Status JavaScript Style Guide Code Climate

forthebadge forthebadge

forthebadge

string-format-validation

Javascript library to format & validate strings (or Numbers)

============================

A common usage for this library is <input /> fields and validating the input and/or formatting (masking ie: phone (###) ###-####).

Built Atop Awesome Work By Others

validator.js string-mask

============================

Usage

npm install string-format-validation

Format (masking)

import StringFormatValidation from 'string-format-validation'

const userInput = '9166163600'
const format = '(###) ### - ####'
const formattedInput = StringFormatValidation.format(format, userInput)

console.log(formattedInput) // (916) 616 - 3600

Validate

import StringFormatValidation from 'string-format-validation'

const validationRules = {
  name: {
    min: 1,
    max: 20
  },
  email: {
    type: 'email'
  },
  zip: {
    size: 5
  }
}

const userInput1 = 'Cory'
const userInput2 = 'coryrobinson42@gmail.com'
const userInput3 = '90210'
console.log( StringFormatValidation.validate(validationRules.name, userInput1) ) // returns `true`
console.log( StringFormatValidation.validate(validationRules.email, userInput2) ) // returns `true`
console.log( StringFormatValidation.validate(validationRules.email, userInput3) ) // returns `true` because the string length is '===' 5

Validation

Available rules: (Please open an issue to request a specific rule)

min {integer} // minimum length

max {integer} // max length

size {integer} // exact length

type

  • date
  • email
  • phone
  • creditcard
  • number

Rules must be in Object structure:

{
  min: 1, // min characters allowed
  max: 30 // max characters allowed
}

{
  size: 10,
  type: 'phone' // any of the listed 'types' above
}

Format

You can use Special Characters for your format needs.

import StringFormatValidation from 'string-format-validation'

const userInput = '9166163600'
const format = '(###) ### - ####'
const formattedInput = StringFormatValidation.format(format, userInput)

console.log(formattedInput) // (916) 616 - 3600
CharacterDescription
0Any numbers
9Any numbers (Optional)
#Any numbers (recursive)
AAny alphanumeric character
aAny alphanumeric character (Optional) Not implemented yet
SAny letter
UAny letter (All lower case character will be mapped to uppercase)
LAny letter (All upper case character will be mapped to lowercase)
$Escape character, used to escape any of the special formatting characters.

Putting It Together - Validate & Format

import StringFormatValidation from 'string-format-validation'

const rules = {
  name: {
    min: 1,
    max: 20
  },
  email: {
    type: 'email'
  },
  phone: {
    format: '(###) ### - ####'
    size: 13 // must include formatting added characters ie: '()' & '-'
  }
}

const userInputs = {
  name: 'cory',
  email: 'coryrobinson42', // not a valid email
  phone: '9166163600'
}

console.log( StringFormatValidation(rules, userInputs) )
/* returns:
  {
    name: true,
    email: false,
    phone: {
      valid: true,
      format: '(916) 616 - 3600'
    }
  }
*/

Note:

When invoking StringFormatValidation(rulesMap, userInputMap) rulesMap can be an object of keys that contain the rules for the respective key matching up to the key in userInputMap, ie:

const rulesMap = {
  // this key declares the rules (format/validation) for 'firstname' key in `userInputMap`
  firstName: {
    max: 20
  }
}

const userInputMap = {
  // this key string is matched against the rules in `rulesMap` under the same key
  firstName: 'cory'
}

StringFormatValidation(rulesMap, userInputMap)
// returns: { firstName: true } <-- validation

You can also simply pass an Object of validation/format rules as the first argument and a string as the second argument, ie:

StringFormatValidation({ type: phone, format: '(###) ###-####' }, '(916) 61')
// returns: { valid: false, format: '(916) 61'}

===========================

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100