Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View adammcarth's full-sized avatar

Adam McArthur adammcarth

View GitHub Profile
function truncate(string, words) {
var max_chars = 6 * parseInt(words);
var words_array = string.split(" ");
var index = parseInt(words) - 1;
var truncated = words_array.slice(0, index).join(" ");
if ( truncated.length > max_chars ) {
truncated = string.slice(0, max_chars) + "...";
} else if ( truncated === string ) {
truncated = string;
@adammcarth
adammcarth / script.php
Last active August 29, 2015 14:07
Validate form with PHP and Regular Expressions
<?php
// Presence check
if ($firstname == '' || $lastname == '' || $street == '' || $suburb == '' || $postcode == '' || $email == '' || $status == '' || $dob == '') {
$error = 'Your field is empty';
renderForm($firstname, $lastname, $street, $suburb, $postcode, $email, $status, $dob, $error);
// Validate Email
} elseif (ereg('\S+@\S+\.\S+', $email) == false) {
$error = 'Please enter a valid email address.';
renderForm($firstname, $lastname, $street, $suburb, $postcode, $email, $status, $dob, $error);
// Validate using a custom URL (external server script)
function validates_instance_url( url, attr ) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if ( xhr.readyState === 4 ) {
if ( xhr.status !== 200 || xhr.responseText === "false" || xhr.responseText === "0" ) {
return false;
} else {
return true;
}
// ......................//
// Database configuration
// ......................//
// Establish connection to the MySQLite database file...
var db_connection = require("knex")({
client: "sqlite3",
connection: {
filename: "../blog_database.db" // path to sqlite database we just created
}
$ sqlite3 blog_database.db
sqlite3> CREATE TABLE Posts(
...> id INTEGER PRIMARY KEY AUTOINCREMENT,
...> title VARCHAR(255),
...> content TEXT,
...> created_at TEXT
...> );
sqlite3> .quit
// Routes
var blogRoutes = require("./routes/blog.js");
app.use("/", blogRoutes);
// Require the express library
var express = require("express");
var router = express.Router();
router.get("/", function(request, response) {
response.render("index", { // "index" is the name of the view file to render (without extension)
title: "Welcome to my blog!" // Will be used for the HTML <title>
});
});
// Require express to handle our http stuff later on.
var express = require("express");
// Require EJS - our templating system for views.
var ejs = require("ejs");
ejs.open = "<?";
ejs.close = "?>"; // Use javascript in view files with <? ... ?>
// Express can be used under the `app` namespace.
var app = express();
@adammcarth
adammcarth / index.html
Created July 19, 2014 04:21
Truncation using CSS.
<div class="container">The quick brown fox jumps over the lazy dog.</div>
def truncate(string, words)
# Define the maximum number of characters that can be used before
# standard truncation is applied (prevents long words from ruinning HTML).
max_chars = 6 * words
words_array = string.split(" ") # splits each word into an array ["like", "this"]
index = words - 1 # array indexes start at 0, so let's take one number off the words amount.
truncated = words_array[0..index].join(" ")
return truncated + "..."
end