1.0.10 • Published 6 years ago

simplest-js-validator v1.0.10

Weekly downloads
1
License
ISC
Repository
-
Last release
6 years ago

Simplest Js Validator

Installation

Manually

	<script src="../validator.js">

Using NPM

	npm install --save simplest-js-validator

How to use

For use name or data attributes see configuration below

HTML

<input id="username"></input>
<div id="usernameErrors" hidden></div>

<!-- Using VueJs -->
<div v-if="hasError('username')">
	<small v-for="e in errors.username">{{ e }}</small>
</div>

JS

let userValidator = {
	username: 'required, email',
	birthday: 'required, regex:^\\d{2}\\/\\d{2}\\/\\d{4}$',
}

let validator = new Validator();
validator.validate(userValidator).then((validation) => {
	if (validation.isValid) {
		// Do something...
	} else {
		validation.errors.forEach((e) => {
			let small = document.createElement('small').text = e;
			document.querySelect('#'+ e + 'Error').append(small);
			document.querySelect('#'+ e + 'Error').removeAttr('hidden');
		})
		
		// ON VueJS
		data {
			errors: []
		}
		...
		methods: {
			hasError(field) {
				return Object.keys(this.errors).indexOf(field) > -1
			}
		}
	}
}).catch((e) => {
	// Ops! Error, verify identifiers
	console.log(e)
})

Configurations

	let validator = new Validator({
		findBy: 'name'
		invalid: '%s is invalid',
        missing: '%s is required'
	})

Available Validators

RuleDescription
alphaFor letters and spaces /^[ a-z]+$/i
emailFor email
regexFor custom regex
requiredAvoid empty field

Here some regex for we place by default on this validator, thansk for nerdsrescueme

Considerations

Thank you, and please do not judge! Give suggestions and help, I will be grateful for your contribution. As soon as I have more time I'll improve this little validator and create as a VueJs component or plugin.

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago