The crossDomain, dataType, validKey options are mostly used when you need to connect to external API endpoint. This example shows how to connect and use the Mailgun API to validate an email address.
The remote URL has to return an encoded JSON of array containing the valid key (the key name can be changed by the validKey option):
or
The validator is ignored if the remote URL responses { "valid": null }
Look at this example if you want to attach more data to the returned value and reuse them later
Improving the performance
By default, the plugin will send an Ajax request to your back-end whenever you enter a character in field. If the back-end takes time to process the request, it might slow down the website. In order to improve the performance, you can use following tips:
By setting the delay option, the Ajax request is only fired once in the specific duration time.
Using threshold option
The threshold option indicates value that asks validators not to perform validations unless the length of field value is greater than this number.
In the snippet code below, the remote validator will not send Ajax request until the username field has at least 5 characters:
Using verbose option
Client first, server last. If the field uses multiple validators, the validators processed in the client should be done first. The remote validator is only processed once the field passes all other validators.
Using the verbose option is solution for this approach. Setting verbose: false will stop validations when there is one failure validator.
In the following code, the remote validator is only processed if the field satisfies all notEmpty, stringLength, regexp validators.
Examples
Basic example
The following example shows how to use a remote back-end to check if a given username is already taken or not.
For example, there is same back-end for validating both username and email address. The back-end uses additional parameter named type to determine which field is going to be validated.
For instance, the registration form need to validate both the username and emails.
Overriding name example
By default, it will be set as the name of field. You can override the name option by using the data-fv-remote-name attribute.Here are two cases which you might need to use this attribute.
Using different names for same field
For example, the Sign up and Profile forms use the same back-end URL to validate the email address which is declared with different name.
In this case, use the same data-fv-remote-name attribute and the back-end will get the same data key.
Using same backend for different fields
Assume that the profile form asks you to update multiple email address (primary, secondary, for example). These emails will be validated by the same backend.
In this case, just use the same data-fv-remote-name attribute for these email address fields.