API METHODS

VALIDATE

Invoke this method validate() to begin validation on the form.

import octaValidate from 'octavalidate-reactjs'

export default function Login() {
//handle form submission
const handleSubmit = function (e) {
//initialize the library
const myForm = new octaValidate('form_login')
//prevent reload
e.preventDefault();
//begin validation on form and check if it passed
if(myForm.validate() === true){
/** process form data here **/
}
}
//return the form
...
}

Return type: boolean


STATUS

This method status() returns the number of validation errors on the form.

//create new instance of the function
const myForm = new octaValidate('my_form');
//get the number of validation errors on the form
myForm.status();

Return type: integer


FORM

This method form() returns the form id attached to the instance.

//create new instance of the function
const myForm = new octaValidate('my_form');
//get form id
myForm.form();

Return type: string


CUSTOM RULE

This method customRule() allows you to construct a single validation rule. To construct a rule, you need the rule's title, regular expression and an error message.

This method returns true on success and false on failure.

This method allows you to construct a single rule. Use the multiple rules method to construct more rules.

//create new instance of the function
const myForm = new octaValidate('my_form');

//Here's the syntax
//myForm.customRule(RULE_TITLE, REG_EXP, ERROR_TEXT);

//custom password validation
const rule_title = "PASS";
const reg_exp = /12345/;
const err_txt = "Please enter 12345";
//build the rule
myForm.customRule(rule_title, reg_exp, err_txt);

Return type: Boolean

Then in your form input, provide the rule title within the octvalidate attribute.

All rule titles are case-sensitive!


MORE CUSTOM RULES

This method moreCustomRules() allows you to construct more custom rules.

To use this method, create an object that will contain the rule title as the object's property, and its value is an array containing the rule's regular expression, and error text separated by a comma.

This method returns true on success and false on failure.

//create new instance of the function
const myForm = new octaValidate('my_form');

//Here's the syntax
//myForm.moreCustomRules(RULES);

//custom password validation
var RULES = {
"PASS": [/12345/, "Please Enter 12345"],
"USERNAME": /simon/, "Please enter simon"]
};
//build the rule
myForm.moreCustomRules(RULES);

Return type: Boolean

Then in your form input, provide the rule title within the octvalidate attribute.

You do not need to pass in your regular expression as a string! This is because the JavaScript engine natively recognizes regular expressions.


CALLBACK

This method allows you to provide additional functions that will be executed when validation tests are passed or when a validation test fails.

To use this method, provide your success callback and error callback as arguments to the method validateCallBack. The success callback will execute when validation tests are passed and the error callback will execute when any validation test fails.

//create new instance of the function
const myForm = new octaValidate('my_form');

//Here's the syntax
//myForm.validateCallBack(SUCCESS_CALLBACK, ERROR_CALLBACK);

//success callback
let successCB = function(){
alert("No validation error");
}
//error callback
let errorCB = function(){
alert(myForm.status() + " validation error(s)")
}
//invoke the method
myForm.validateCallBack(successCB, errorCB);

Return type: function


VERSION

This method returns the version of the release that you are using.

//create new instance of the function
const myForm = new octaValidate('my_form');
//get version
myForm.version();

Return type: string


SHOW & HIDE SERVER-SIDE FORM ERRORS

This method allows the library to display server-side validation errors on the form.

If you must use this Library to process server-side validations, invoke the methods below instead of using the helper script attached to that release.

1. showBackendErrors

Invoke this method and pass in the error object to insert server-side validation errors into the form.

2. removeBackendErrors

Invoke this method and pass in a form id to remove any errors present in the form.

Server-side form validation is built for PHP & NodeJS. Please refer to their documentation.

//create new instance of the function
const myForm = new octaValidate('my_form');
//show server-side validation Errors
myForm.showBackendErrors(error_object);
//remove server-side validation Errors
myForm.removeBackendErrors(form_id);