Wednesday, February 25, 2009

Regular Expresions JavaScript

My list of Regular Expressions used in validating forms using JavaScript
  • Checking Email Addresses,
    remail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;   
  • Checking a persons name,
    rename = /^((?:[A-Z](?:('|(?:[a-z]{1,3}))[A-Z])?[a-z]+)|(?:[A-Z]\.))(?:([ -])((?:[A-Z](?:('|(?:[a-z]{1,3}))[A-Z])?[a-z]+)|(?:[A-Z]\.)))?$/;
  • Checking the contents of a message or text box to make sure that only the characters intend are being used,
    remess = /^[0-9A-Za-z\,\.\'\-\s]+$/;

Sunday, February 22, 2009

Contact form Javascript

This JavaScript has 3 functions, the first checks the details entered into the contact form an should the criteria be met, the form is processed.

The second and third functions count down the characters entered in to a texarea

I use this JavaScript in conjunction with the post Adding PHP Contact Form


function Contact()
{
re = "no";
remail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
rename = /^((?:[A-Z](?:('|(?:[a-z]{1,3}))[A-Z])?[a-z]+)|(?:[A-Z]\.))(?:([ -])((?:[A-Z](?:('|(?:[a-z]{1,3}))[A-Z])?[a-z]+)|(?:[A-Z]\.)))?$/;
remess = /^[0-9A-Za-z\,\.\'\-\s]+$/;
if(rename.test(document.contact.name.value))
{
re="yes";
}
else
{
alert("Please check your name");
document.contact.name.select();
document.contact.name.focus();
return false;
}
if(remail.test(document.contact.email.value))
{
re="yes"
}
else
{
alert("Please check your email address");
document.contact.email.focus();
document.contact.email.select();
return false
}
if(remess.test(document.contact.question.value))
{
re="yes";
}
else
{
alert("Please check your Message");
document.contact.question.select();
document.contact.question.focus();
return false;
}
if(re=="yes")
{
return true
}
}

function getObject(obj)
{
var theObj;
if(document.all)
{
if(typeof obj=="string")
{
return document.all(obj);
}
else
{
return obj.style;
}
}
if(document.getElementById)
{
if(typeof obj=="string")
{
return document.getElementById(obj);
}
else
{
return obj.style;
}
}
return null;
}


function Contar(entrada,salida,texto,caracteres)
{
var entradaObj=getObject(entrada);
var salidaObj=getObject(salida);
var longitud=caracteres - entradaObj.value.length;
if(longitud <= 0)
{
longitud=0;
texto=' '+texto+' ';
entradaObj.value=entradaObj.value.substr(0,caracteres);
}
salidaObj.innerHTML = texto.replace("{CHAR}",longitud);
}

Saturday, February 21, 2009

Named Anchors

A named anchor works like a bookmark on a page, if you have a page with a lot of text and want to browse to a position on the page, a named anchor will help

Below is the link that directs you to the anchor



The anchor should be placed above the text you want to browse to.

Thursday, February 19, 2009

Adding a php contact form

Create a form with the action calling itself, and the method is POST

The form should have 2 input text boxes named, name, email and a textarea named question

Have added some JavaScript information to validate the contents of the form, at this post 

Add the folowing to the start of the php file

$name = $_POST['name'];
$email = $_POST['email'];
$question = $_POST['question'];
$emailto = "email@yourwebsite.com";

Include the following to call a function

include "function_contact_form.php";
contact_form($name,$email,$question,$emailto);

Create a file called function_contact_form.php and add the text below.

function contact_form($name,$email,$question,$emailto)
{
$myemail = $emailto;

$subject = "Ask A Question";

$message = "From: $name ($email) \n
Question: $question \n";

$from = "From: $email\r\n";

$spamerrormessage = "A web site URL has been detected, the form submission has been cancelled";
if (preg_match("/http/i", "$name")) 
{
echo " $spamerrormessage"; 
exit();
}
if (preg_match("/http/i", "$email")) 
{
echo " $spamerrormessage";
exit();
}
if (preg_match("/http/i", "$message")) 
{
echo " $spamerrormessage"; 
exit();
}


if ($myemail !="")
mail ($myemail, $subject, $message, $from);
echo "Thank You $name for your inquiry.";
}