Turn any form to a Jetform :) Demo.
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.
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 |
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 |
Name | Description |
---|---|
reset | Reset the form and restore the original state of the error messages |
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 |
<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>
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"
}
});
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"
}
});
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);
});
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 |
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>
You can merge two inputs by adding data-prefix=#prefix-selector
to the desired input.
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'
}
]
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'
}
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 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 |
$('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');
}
})
$('form').jetform('reset');