/* Program name : form_check.js
Program function : general form checking client side js functions
Author : Arnold Bandwring
Developed by : Daffodils Interactive
sample usage of some of the functions declared below are as follows:
--------------------------------------------------------------------*/
// function to confirm database setup
function confirm_setup(){
if(confirm("Tables already exists.\nThis action will drop all tables and data and it CANNOT BE UNDONE. \nAre you sure that you want to run Setup again?")){
return true;
}
return false;
}
// function to open a new window
function openWin(windowURL, windowName, windowFeatures){
window.open(windowURL, windowName, windowFeatures);
return false;
}
// function to confirm record deletion
function confirmCleanUp(Link) {
if (confirm("Are you sure you want to delete ?")) {
location.href=Link;
}
}
// to simulate the clicking of the browser back button
function fn_back(){
window.history.back();
}
// function to check YEAR with 4 digit
function isYear(snum)
{
var reInteger = /^[1-9]\d{3}$/
return reInteger.test(snum)
}
// function to check indiger field
function IsInteger(snum)
{
var reInteger = /^\d+$/
return reInteger.test(snum)
}
// function to check SSN US
function IsSSN(snum)
{
var reSSN = /^(\d{3})(-)(\d{2})(-)(\d{4})$/
return reSSN.test(snum)
}
// function to check US phone - fomat 999-999-9999
function IsPhone(snum)
{
var rePhone = /^(\d{2})([- ])(\d{4})([- ])(\d{4})$/
//var rePhone = /^(\+[1-9]{1,4}(\s|\-)?)?\s?\(?\d\ d\)?\s?\d\d\d\d(\s|\-)?\d\d\d\d$/
//var rePhone = /^((\+[1-9]{1,4}[ \*)|(\([0-9]{2,3}\)[ \*)|([0-9]{2,4})[ \*)*?[0-9]{3,4}?[ \*[0-9]{3,4}?$/
return rePhone.test(snum)
}
function IsMobile(snum)
{
var rePhone = /^(\d{4})([- ])(\d{3})([- ])(\d{3})$/
//var rePhone = /^(\+[1-9]{1,4}(\s|\-)?)?\s?\(?\d\ d\)?\s?\d\d\d\d(\s|\-)?\d\d\d\d$/
//var rePhone = /^((\+[1-9]{1,4}[ \*)|(\([0-9]{2,3}\)[ \*)|([0-9]{2,4})[ \*)*?[0-9]{3,4}?[ \*[0-9]{3,4}?$/
return rePhone.test(snum)
}
function AusPhone(snum)
{
var totphonenum="";
var totphonelen =snum.length;
for ( var i=0;i 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
}
}
return true; // date is valid
}
// End date validation ------------------------------------->
function emailCheck(emailStr) {
/* The following pattern is used to check if the entered e-mail address
fits the user@domain format. It also is used to separate the username
from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
characters. We don't want to allow special characters in the address.
These characters include ( ) < > @ , ; : \ " . [ ] */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a
username or domainname. It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes). E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
rather than symbolic names. E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
/* Finally, let's start trying to figure out if the supplied address is
valid. */
/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */
alert("Email address seems incorrect (check @ and .'s)")
return false
}
var user=matchArray[1]
var domain=matchArray[2]
// See if "user" is valid
if (user.match(userPat)==null) {
// user is not valid
alert("The username doesn't seem to be valid.")
return false
}
/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
// this is an IP address
for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Destination IP address is invalid!")
return false
}
}
return true
}
// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
alert("The Domain Name doesn't seem to be valid.")
return false
}
/* domain name seems valid, but now make sure that it ends in a
three-letter word (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding
the domain or country. */
/* Now we need to break up the domain to get a count of how many atoms
it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 ||
domArr[domArr.length-1].length>3) {
// the address must end in a two letter or three letter word.
alert("The address must end in a three-letter domain, or two letter country.")
return false
}
// Make sure there's a host name preceding the domain.
if (len<2) {
var errStr="This E-mail address is missing a hostname!"
alert(errStr)
return false
}
// If we've gotten this far, everything's valid!
return true
}
/////////////
function FormatNumber(num, decimalNum, bolLeadingZero, bolParens)
/* IN - num: the number to be formatted
decimalNum: the number of decimals after the digit
bolLeadingZero: true / false to use leading zero
bolParens: true / false to use parenthesis for - num
RETVAL - formatted number
*/
{
var tmpNum = num;
// Return the right number of decimal places
tmpNum *= Math.pow(10,decimalNum);
tmpNum = Math.floor(tmpNum);
tmpNum /= Math.pow(10,decimalNum);
var tmpStr = new String(tmpNum);
// See if we need to hack off a leading zero or not
if (!bolLeadingZero && num < 1 && num > -1 && num !=0)
if (num > 0)
tmpStr = tmpStr.substring(1,tmpStr.length);
else
// Take out the minus sign out (start at 2)
tmpStr = "-" + tmpStr.substring(2,tmpStr.length);
// See if we need to put parenthesis around the number
if (bolParens && num < 0)
tmpStr = "(" + tmpStr.substring(1,tmpStr.length) + ")";
return tmpStr;
}
// end o
// ==========================================================================
// Fuctions to mimic LTrim, RTrim, and Trim...
// ==========================================================================
function LTrim(str)
{
var whitespace = new String(" \t\n\r");
var s = new String(str);
if (whitespace.indexOf(s.charAt(0)) != -1) {
// We have a string with leading blank(s)...
var j=0, i = s.length;
// Iterate from the far left of string until we
// don't have any more whitespace...
while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
j++;
// Get the substring from the first non-whitespace
// character to the end of the string...
s = s.substring(j, i);
}
return s;
}
// --------------------------------------------------------------------------
// Remove trailing blanks from our string.
// I str - the string we want to RTrim
// Return the input string without any trailing whitespace
// Date 03 avr. 2003 23:13:50
// Author Aurélien Tisné (CS)
// --------------------------------------------------------------------------
function RTrim(str)
{
// We don't want to trip JUST spaces, but also tabs,
// line feeds, etc. Add anything else you want to
// "trim" here in Whitespace
var whitespace = new String(" \t\n\r");
var s = new String(str);
if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
// We have a string with trailing blank(s)...
var i = s.length - 1; // Get length of string
// Iterate from the far right of string until we
// don't have any more whitespace...
while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
i--;
// Get the substring from the front of the string to
// where the last non-whitespace character is...
s = s.substring(0, i+1);
}
return s;
}
function Trim(str)
{
return RTrim(LTrim(str));
}
function postcode(s)
{
if(isEmpty(s))
{
return false;
}
else
{
if(IsInteger(s))
{
var lnumval=s;
var lnumlength=lnumval.length;
if (lnumlength!=4)
{
return false;
}
}
else
{
return false;
}
}
return true;
}
function check_SpclChar(fldname){
var iChars = "!@#$%^&*_()+=[]\\;,./{}|\":<>?";
for (var i = 0; i < fldname.length; i++) {
if (iChars.indexOf(fldname.charAt(i)) != -1) {
alert ("The box has special characters. \nThese are not allowed.");
return false;
}
}
return true;
}
// EOF