Skip to content

Commit

Permalink
Major updates:
Browse files Browse the repository at this point in the history
- add number of tries option
- resolved issue with multiple instances (#15)
- code improvements
- options more organized
- readme improvements
- demo updated
- add basic unit test with Jest
- drop support for IE10
  • Loading branch information
robiveli committed May 24, 2020
1 parent 10aeb74 commit fa14370
Show file tree
Hide file tree
Showing 12 changed files with 6,645 additions and 952 deletions.
17 changes: 10 additions & 7 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"presets": [
["@babel/preset-env", {
"targets": {
"browsers": [
"last 5 versions",
"Explorer >= 10"
]
[
"@babel/preset-env",
{
"targets": {
"browsers": [
"last 5 versions",
"Explorer >= 11"
]
}
}
}]
]
]
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
npm-debug.log
.DS_Store
coverage/
.DS_Store
75 changes: 49 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# JavaScript Captcha #
### Simple captcha component (~2KB) written in pure JavaScript with no dependencies ###
### Simple captcha component (<2KB) written in pure JavaScript with no dependencies ###

Easy to resolve random math addition rendered within basic canvas element.
Simple numeric captcha rendered within basic [canvas element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas).

### Demo ###

Demo can be seen [here](https://www.rvdizajn.com/js-captcha/).

### Install ###

```
npm install js-captcha
npm install js-captcha --save
```

### Usage ###
Expand All @@ -19,8 +23,7 @@ or
```
import jCaptcha from 'jCaptcha';
```

Set captcha input element:
Define main captcha input element in HTML:
```
<input class="jCaptcha" type="text" placeholder="Type in result please">
```
Expand All @@ -29,53 +32,73 @@ Initialize it:
```
<script>
var myCaptcha = new jCaptcha({
el: '.jCaptcha',
canvas: {
class: 'jCaptchaCanvas',
style: {
// required properties for captcha stylings:
width: 100,
height: 15,
textBaseline: 'top',
font: '15px Arial',
textAlign: 'left',
fillStyle: '#ddd'
}
},
// set callback function for success and error messages:
callback: ( response, $captchaInputElement ) => {
callback: ( response, $captchaInputElement, numberOfTries ) => {
if ( response == 'success' ) {
// success
// success handle, e.g. continue with form submit
}
if ( response == 'error' ) {
// error
// error handle, e.g. add error class to captcha input
if (numberOfTries === 3) {
// maximum attempts handle, e.g. disable form
}
}
}
});
</script>
```

Call `validate()` method when required (e.g. on submit event):
And then call `validate()` method when required (e.g. on form submit event):
```
<script>
myCaptcha.validate();
</script>
```

No extra styling included, just style it as you wish, see options below.
No stylings included, just style it as you wish, see options below.

### Options ###

jCaptcha can take an optional parameters - an object of key/value settings:
jCaptcha can take an optional parameter - an [Object] of key/value settings:

Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| el | false | [String] | 'jCaptcha' | CSS class for input element |
| requiredValue | false | [String] | '*' | Render new random numbers on error validate |
| resetOnError | false | [Boolean] | true | Mandatory field indicator |
| focusOnError | false | [Boolean] | true | Focus input field on error validate |
| clearOnSubmit | false | [Boolean] | true' | Clear input value on every validate |
| callback | false | [Function] | null | As invoked function these useful arguments are returned: response (type: *String*, value: *'success'* or *'error'*), captcha (type: *Element*) and number of tries (type: *Number*) |
| canvasClass | false | [String] | 'jCaptchaCanvas' | CSS class of canvas captcha
| canvasStyle | true | [Object] | {} | Canvas stylings object, required for canvas appearance |
| canvasStyle.width | false | [Number] | null | Width of canvas captcha element (in px) |
| canvasStyle.height | false | [Number] | null | Height of canvas captcha element (in px) |
| canvasStyle.font | false | [String] | '' | Font size and font family of canvas captcha element |
| canvasStyle.fillStyle | false | [String] | '' | Text color of canvas captcha element |
| canvasStyle.textAlign | false | [String] | '' | Text align of canvas captcha element |
| canvasStyle.textBaseline | false | [String] | '' | Text baseline of canvas captcha element |

- **el** String *(default:jCaptcha)* - css class for input element
- **requiredValue** String *(default:\*)* - required filed indicator
- **resetOnError** Boolean *(default:true)* - render new random numbers on error validate
- **focusOnError** Boolean *(default:true)* - focus input field on error validate
- **clearOnSubmit** Boolean *(default:true)* - clear input value on every validate
- **canvasWidth** Number *(default:50)* - width of canvas captcha element (in px)
- **canvasHeight** Number *(default:15)* - height of canvas captcha element (in px)
- **canvasFontSize** String *(default:15px)* - font size of canvas captcha element
- **canvasFontFamily** String *(default:Arial)* - font family of canvas captcha element
- **canvasFillStyle** String *(default:#ddd)* - text color of canvas captcha element
- **callback** Object *(default:null)* - as invoked function these useful arguments are returned: *response* and *captcha input node*

### API ###

`reset()` - generate new random numbers
`reset()` - generate and render new random numbers

`validate()` - validate entered result in input field

### Demo ###

Demo available [here](https://www.rvdizajn.com/js-captcha/).

### Browser support ###

Expand Down
Loading

2 comments on commit fa14370

@PikachuEXE
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the tag is 1.3.0 not v1.3.0?

@robiveli
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By mistake, resolved

Please sign in to comment.