Code Monkey home page Code Monkey logo

easyjetform's Introduction

easyJetform - v3.0.17

Turn any form to a Jetform :) Demo.

Important

jQuery:
jQuery is required to work (any version is acceptable)

Markup:
All the inputs should be wrapped in a container (e.g div) if you wish to present errors under each input.
Only inputs with name property will be processed.

Options

Name Type Description
token String The authentication token as revieved from Jetform. Default: ''
errorSelector String The element selector in which the errors will appear. Default: false
successSelector String The element selector in which the success message will appear. Default: false
hideSuccessAfter Integer Number of seconds to display the success message. Default: false
redirect Object / String Redirect to a specific path after a cretin timeout. Default: false
autoValidate Boolean Should perform a validation immediately when an instance is created. Default: false
autoSend Boolean Should send the data immediately when an instance is created. Default: false
resetErrorEvent String The gesture which will trigger the error reset (e.g click / keypress). Default: false
live Boolean Live validation mode. errors are displayed as you type. Default: false
liveEvent String The event which will trigger the validation on each field. Default: 'keyup'
autoAlign Boolean Should automatically align the text according to the placeholder's or the value's direction. Default: true
telMaxLength Integer The maximum length of inputs of type tel
url String The URL to which the form should be submited. Default: '//jetform.interjet.co.il/lead/save'
privacyMode Boolean Removes identifiable metadata. default: false
requestType String The type of the HTTP request that should be sent. Default: 'text'
template Object The validation engine messages. You can add new or overwrite the existing messages
responses Array Array of possible responses definition. See detailed explanation bellow.
spinner Object Properties for the spinner
permit Object Permission for input characters to a specific input type

Events

Name Description
beforeSubmit Triggered before sending the input data to Jetform and after a successful validation
onSuccess Triggered after a successful response came from Jetform
onError Triggered if errors were found in the form validation
onFail Triggered when an invalid response came from Jetform or a 404 HTTP response status is received

Methods

Name Description
reset Reset the form and restore the original state of the error messages

Validation rules

Jetform plugin include a validation parser and a custom error message template engine.
Validation rules are a string list separated by a pipe “|”.

Name Description
min_length[2] The minimal allowed length of the input's value
max_length[5] The maximum allowed length of the input's value
exact_length[6] The exact allowed length of the input's value
required The field is required and must have a value
integer The value of the field must be numeric only
alpha The value of the field must contain alphbetic characters only
min_words[2] The minimal allowed number of words in the input's value
max_words[5] The maximum allowed number of words in the input's value
regexp[^[A-Za-z\s]+$] The value must match the supplied regular expression
valid_email The value of the field must be a valid email
valid_url The value of the field must be a valid URL (Strict mode)
valid_url[true] The value of the field must be a valid URL (Loose mode)
valid_id_number The value of the field must contain a valid id number
equals[selector] The value of the field must be equal to another field's value
in_list[a,b,c] The value of the field must be one of the items
greater_than[3] The minimum allowed value for the input
greater_than_equal_to[4] Value must be greater or equal to
less_than[6] The maximum allowed value for the input
less_than_equal_to[12] Value must be less or equal to
Validation example
<div class="form-group">
    <label for="firstName">Firstname</label>
    <input type="text" id="first_name" name="jf_txt_1" data-validate="min_length[2]|max_length[4]">
</div>
Function based validation rules

You can easily create your own validation function.
The validation function should be avaliable in the window scope.
Let's create a new function which checks the value of a given field:

function is_black(element) {
    return element.val() == '#000';
}

Now, we can start using it as a validation rule:

<div class="form-group">
    <label for="myColor">Choose color</label>
    <input type="text" id="myColor" name="colorPicker" data-validate="is_black">
</div>

Optionally, we can set an appropriate error message for the new rule:

$('form').jetform({
    template: {
        is_black: "{$field} is not black"
    }
});
Custom error messages

It is also possible to define a custom error message (ignoring the actual error)
This error message will pop every time a validation rule failed to validate the field's value

<div class="form-group">
    <label for="fullname">Fullname</label>
    <input type="text" id="fullname" name="colorPicker" data-error="custom_error" data-validate="required|min_words[2]">
</div>

Don't forget to define the custom message's template:

$('form').jetform({
    template: {
        custom_error: "{$field} contain invalid value"
    }
});
Validation error detection

There are 2 ways to detect errors when they occure:
Class level event: onError event will be triggered with an array of errors as a function argument

onError: function(errors){
    console.log(errors);
}

Element level event: jetform.error event will be triggered with an error object as a function argument.
Since each time only one validation error is desplayed for each field, the current error object will be passed as an argument.

$('#full_name').on('jetform.error', function(event, error){
    console.log(error);
});
Error object

Each error is an object with the following properties:

Name Description
field A jQuery element object
rule The name of the validation rule which failed the validation process
value The value given in the rule for rules with values (otherwise equal to null)
message A human readable message taken from the template after being parsed by the validation engine
Extending the validation engine

You can easily extend the validation engine by teaching it new validation rules.
Before creating a Jetform instance add a new validation rule by extending the Jetform.Utils interface:

Jetform.Utils.validations.is_dog = function(element){
    return element.val() == 'woof woof';
}

After creating the new validation rule, we need to setup the appropriate error message:

$('form').jetform({
    template: {
        is_dog: "{$field} is not a dog"
    }
});

Once we finished we can start using out new validation rule:

<div class="form-group">
    <label for="firstName">Fullname</label>
    <input type="text" id="full_name" name="jf_txt_1" data-validate="required|is_dog">
</div>

prefix support

You can merge two inputs by adding data-prefix=#prefix-selector to the desired input.

responses

Array of objects. Each object contain a definition for a possible response
. The Keys available for each object are:

Name Description
key String - For text matching. Function for custom validation, should return boolean
gtm_event String. If defined a GTM event will be triggered with the collected data as variables
preCallback String. Internal method to run before calling the callback
postCallback String. The callback to run after the rule being matched, as defined in the options
arguments Array. Array of public members to pass into the postCallback
template String. The template of the response message passed into the postCallback as defined in the options

The default values match the responses of the Jetform system since this plugin serves the Jetform platform.
This dynamic option is to allow users to use the plugin with other platforms.

Defaults:

responses: [
    {
        key: 'success',
        gtm_event: 'jetform_submit_success',
        preCallback: 'displaySuccess',
        postCallback: 'onSuccess',
        arguments: ['args']
    },
    {
        key: 'reason=unique',
        postCallback: 'onFail',
        template: 'unique'
    }
]

spinner

You can control over the spinner that is shown in the input type submit while the data is being sent to Jetform.
The avaliable properties are:

Name Description
active Boolean.
width String. The width of the spinner (e.g. 20px)
height String. The height of the spinner (e.g. 20px)
color String. Hexadecimal value for the color of the spinner.
spinner: {
    active: true,
    width: '30px',
    height: '20px',
    color: '#333'
}

permit

You can disable certain characters from being typed in a certain input type.
The permission is given by specifying a regular expression and an event to check the expression against the values of the input (usually it will be done on keypress).

permit: {
    email: {
        rule: /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/,
        event: 'keypress'
    }
}

Jetform.Utils interface

Jetform.Utils is a static interface which is accessible from anywhere.
The interface contains several general purpose methods and properties:

Name Type Description
queryString Method Get a query string parameter
validations Object The validation rules (functions) used by the engine
postCORS Method Send an HTTP Cross-Origin POST request using xmlHttpRequest 2.0
getCORS Method Send an HTTP Cross-Origin GET request using xmlHttpRequest 2.0

Full example

$('form').jetform({
    token:'nZq6scKaNvcXdgjszIcN1kaHhbYDKjAAie0yPKyTVU4AiE0Aiv9VGKu0sH7fVqWhqEkRvUyhbApBpYRGmgPkZA==',
    errorSelector: '.input-error',
    resetErrorEvent: 'keypress',
    onSuccess: function(args, response){
        alert('I am the king of the world!');
    },
    onError: function(errors){
        alert('Ooops... something went wrong');
    }
})

Method calling example

$('form').jetform('reset');

easyjetform's People

Contributors

chaimsiegel avatar ethankore avatar interjet avatar israelvainberg avatar ronka avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

qa-tester22

easyjetform's Issues

resetFieldError fires on empty not required fields

resetFieldError runs on all fields even those that dont have required/any validation.
because of that if its an email input for example it will mark it as invalid even if its empty and not required.

onError doesn't pass the server error

For example with Jetform, when a field is specified as unique the error passed onError is the general error even though the server return something specific

URL Parameter

I want to be able to send the data to a different url and process the data in my own script.

The url parameter of the form should be an option of the plugin.
If the url option is missing the plugin should take the action attribute of the form.
If the form has not action parameter and the url option is missing there should be a default url to jetform.interjet.co.il

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.