API METHODS

Learn about the methods that can be invoked in this library
VALIDATE

Invoke this method to begin validation on the form.

<script>
//create new instance of the function
const myForm = new octaValidate('my_form');
//listen for submit event
document.querySelector('#my_form').addEventListener('submit', function(e){
e.preventDefault();
//invoke the method
if(myForm.validate() === true){
//validation successful, process form data here
} else {
//validation failed
}
})</script>

Return type: boolean


STATUS

This method returns the number of validation errors on the form.

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

Return type: integer


FORM

This method returns the form id attached to the instance.

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

Return type: string


CUSTOM RULE

This method 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.

<script>
//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);
</script>

Return type: Boolean

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

All rule titles are case-sensitive!

<form id="my_form">
<input octavalidate="R,PASS" id="inp_pass" type="password">
<button type="submit">submit</button>
</form>
DEMO FORM
12345 is the key

MORE CUSTOM RULES

This method 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.

<script>
//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);
</script>

Return type: Boolean

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

<form id="my_form">
<input octavalidate="R,PASS" id="inp_pass" type="password">
<input octavalidate="R,USERNAME" id="inp_uname" type="text">
<button type="submit">submit</button>
</form>
DEMO FORM
12345 is the key

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.

<script>
//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);
</script>

Return type: function

DEMO FORM
Try to submit without a value and with a value

VERSION

This method returns the current version of this Great Library.

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

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 these methods instead of using the helper script attached to the library.

showBackendErrors

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

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.

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

DEMO FORM