0.0.1 • Published 3 years ago
reprehendo v0.0.1
Reprehendo
Library for form validation
Example
HTML
<form>
<!-- Registering the key firstName -->
<input type="text" data-reprehendo="firstName" placeholder="First name">
<!-- Registering the key lastName -->
<input type="text" data-reprehendo="lastName" placeholder="Last name">
<!-- Registering the key password -->
<input type="password" data-reprehendo="password" placeholder="password">
<button type="submit">Submit</button>
</form>
JS
import Reprehendo from 'reprehendo';
new Reprehendo({
firstName: {
type: 'input',
invalidClass: 'invalid',
validClass: 'valid',
required: true,
bigFirstSymbol: true
},
lastName: {
type: 'input',
invalidClass: 'invalid',
validClass: 'valid',
required: true,
bigFirstSymbol: true
},
password: {
type: 'password',
required: true,
contentPassword: ['*', '$'],
validClass: 'valid',
invalidClass: ['invalid', 'invalid_2'],
minLength: 3,
maxLength: 30
}
}).init();
Thus
Use the data-reprehendo attribute to register keys
Pass the options you need for validation to these objects
Note
You will have access to some options that are suitable for certain types. For example, the "contentPassword" option will be available for the password, but not for other types.
List of types for elements
Type | Value | Example |
---|---|---|
input | The most basic type | type: 'input' |
password | Suitable for password | type: 'password' |
Suitable for email | type: 'email' | |
number | Suitable for number | type: 'number' |
repeatPassword | Suitable for repeat password | type: 'repeatPassword' |
checkbox | Suitable for checkbox (adopting) | type: 'checkbox' |
select | Suitable for "select" tag | type: 'select' |
List of options
Option | Type | Value | What type is it suitable for | Example |
---|---|---|---|---|
required | Boolean | Mandatory input field | all | required: true |
pattern | Regexp | Accepts a regular expression that tests the element along with other options | phone, input, password | pattern: /[a-z]*/ |
chooseOption | Object | What should be the value of select | select | chooseOption: ['Russia', 'USA'] |
minLength | Number | Minimum number of characters | input, password, number | minLength: 3 |
maxLength | Number | Maximum number of characters | input, password, number | maxLength: 3 |
bigFirstSymbol | Boolean | Starts with a large first character | input | bigFirstSymbol: true |
contentPassword | Object | What characters the password should contain | password | contentPassword: ['*', '!'] |
classValid | String or Object | Content is added when the element is valid | all | classValid: 'valid' or classValid: ['valid', 'valid_2'] |
classInvalid | String or Object | Content is added when the element is invalid | all | classInvalid: 'valid' or classInvalid: ['invalid', 'invalid_2'] |
noSpace | Boolean | Doesn't allow spaces | input, password, number | noSpace: true |
giveData | Object | Function for accepting data. Fires when all elements are valid | global object | { giveData(data) {...}, password: { type: 'password' ... } } |
repeatAt | String | Accepts a key for which the password will be repeated | repeatPassword | repeatAt: 'password' |
view | String | Defines the appearance of the string | number | view: 'default' or 'card' or 'date' |
Examples of some options
chooseOption
HTML
<form>
<select data-reprehendo="country" name="country">
<option value="Select your country">Select your country</option>
<option value="Russia">Russia</option>
<option value="USA">USA</option>
</select>
<button type="submit">Submit</button>
</form>
JS
import Reprehendo from 'reprehendo';
new Reprehendo({
country: {
type: 'select',
required: true,
invalidClass: 'invalid',
validClass: 'valid',
chooseOption: ['Russia', 'USA']
}
}).init();
pattern
HTML
<form>
<input type="text" data-reprehendo="password" />
<button type="submit">Submit</button>
</form>
JS
import Reprehendo from 'reprehendo';
new Reprehendo({
password: {
type: 'password',
required: true,
minLength: 6,
// See here
pattern: /\d+/ // Only numbers
}
}).init();
giveData
HTML
<form>
<input type="text" data-reprehendo="firstName" placeholder="First name">
<input type="text" data-reprehendo="lastName" placeholder="Last name">
<button type="submit">Submit</button>
</form>
JS
import Reprehendo from 'reprehendo';
new Reprehendo({
firstName: {
type: 'input',
invalidClass: 'invalid',
validClass: 'valid',
required: true,
minLength: 3,
bigFirstSymbol: true,
noSpace: true
},
lastName: {
type: 'input',
invalidClass: 'invalid',
validClass: ['valid', 'valid_2'],
minLength: 3,
required: true,
bigFirstSymbol: true,
noSpace: true
},
// See here
giveData(data) {
console.log(data); // { lastName: element values, firstName: element values }
},
}).init();
view
HTML
<form>
<input type="text" data-reprehendo="number" placeholder="Number">
<button type="submit">Submit</button>
</form>
JS
import Reprehendo from 'reprehendo';
new Reprehendo({
number: {
type: 'number',
validClass: 'valid',
invalidClass: 'invalid',
// See here
view: 'date' // 10/10/2021,
view: 'default' // 123456... only numbers,
view: 'card' // 0000 0000 0000 0000 - view credit card
}
}).init();
0.0.1
3 years ago