Skip to content

Instantly share code, notes, and snippets.

View Octagon-simon's full-sized avatar
Turning red flags into red tests that pass.

Simon Ugorji (Octagon) Octagon-simon

Turning red flags into red tests that pass.
View GitHub Profile
<?php
//define validation rules
$valRules = array(
"username" => array(
["R", "Your username is required"]
),
"email" => array(
["R", "Your Email is required"],
["EMAIL", "Your Email is invalid"]
),
<?php
//syntax for defining validation rules
$valRules = array(
"FORM_INPUT_NAME" => array(
["RULE_TITLE", "CUSTOM_ERROR_MESSAGE"]
)
);
?>
<?php
//include the validation file
require 'octaValidate-php/src/Validate.php';
use Validate\octaValidate;
//create new instance of the class
$DemoForm = new octaValidate('form_demo');
?>
<html>
<body>
<form id="form_demo" method="post" novalidate>
<label>Username</label><br>
<input name="username" type="text" id="inp_uname"> <br>
<label>Email</label><br>
<input name="email" type="email" id="inp_email"> <br>
<label>Age</label><br>
<input name="age" type="number" id="inp_age"> <br>
//store required extensions
const requiredExts = [".png", ".jpg", ".jpeg"];
//store error
const valError = [];
//loop through object
for (let file of fileInpFiles){
//retrieve extension from name
const ext = file.name.substring(file.name.lastIndexOf('.'));
//check if it matches with the required extension
if ( !requiredExts.includes( ext ) ){
//create an array that will store the image file type
const requiredTypes = ["image/jpg", "image/jpeg", "image/png"];
//store error
const valError = [];
//loop through object and compare type
for (let file of fileInpFiles){
if ( !requiredTypes.includes( file.type ) ){
valError.push(file.name);
}
}
//convert 2mb to bytes
const requiredSize = 1024 * 1024 * 2;
//save file size
let filesSize = 0;
//loop through object
for (let file of fileInpFiles){
//add up file sizes
filesSize+=file.size;
}
//check if files size is greater than 2MB or 2,000,000 bytes
//loop through object
for (let file of fileInpFiles){
//log current file
console.log(file);
}
//retrieve file input element
const fileInp = document.querySelector('#inp_file');
//retrieve file
const fileInpFiles = fileInp.files
//log result
console.log(fileInpFiles);
<form novalidate>
<input type="file" id="inp_file" accept=".png" multiple><br><br>
<button type="submit">Upload</button>
<form>