// validation.js
// combination of dateValidation.js, formatvalidation.js and commonValidation.js
//<!-- This script and many more are available free online at -->
//<!-- The JavaScript Source!! http://javascript.internet.com -->
//<!-- V1.1.3: Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
//<!-- Original: Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
//<!-- Changes:
/* 1.1.4: Fixed a bug where upper ASCII characters (i.e. accented letters
international characters) were allowed.
1.1.3: Added the restriction to only accept addresses ending in two
letters (interpreted to be a country code) or one of the known
TLDs (com, net, org, edu, int, mil, gov, arpa), including the
new ones (biz, aero, name, coop, info, pro, museum). One can
easily update the list (if ICANN adds even more TLDs in the
future) by updating the knownDomsPat variable near the
top of the function. Also, I added a variable at the top
of the function that determines whether or not TLDs should be
checked at all. This is good if you are using this function
internally (i.e. intranet site) where hostnames don't have to 
conform to W3C standards and thus internal organization e-mail
addresses don't have to either.
Changed some of the logic so that the function will work properly
with Netscape 6.
1.1.2: Fixed a bug where trailing . in e-mail address was passing
(the bug is actually in the weak regexp engine of the browser; I
simplified the regexps to make it work).
1.1.1: Removed restriction that countries must be preceded by a domain,
so abc@host.uk is now legal. However, there's still the 
restriction that an address must end in a two or three letter
word.
1.1: Rewrote most of the function to conform more closely to RFC 822.
1.0: Original */
// -->
function isEmail (emailStr)
{
/*
* The following variable tells the rest of the function whether or not to
* verify that the address ends in a two-letter country or well-known TLD. 1
* means check it, 0 means don't.
*/
var checkTLD=0;
/* The following is the list of known TLDs that an e-mail address must end with. */
var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
/*
* 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];
// Start by checking that only basic ASCII characters are in the strings
// (0-127).
for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("Ths username contains invalid characters.");
return false;
}
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("Ths domain name contains invalid characters.");
return false;
}
}
// 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. Check if it's valid.
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("The domain name does not seem to be valid.");
return false;
}
}
/*
* domain name seems valid, but now make sure that it ends in a known top-level
* domain (like com, edu, gov) or a two-letter word, representing country (uk,
* nl), and that there's a hostname preceding the domain or country.
*/
if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1)
{
alert("The address must end in a well-known domain or two letter " + "country.");
return false;
}
// Make sure there's a host name preceding the domain.
if (len<2) {
alert("This address is missing a hostname!");
return false;
}
// If we've gotten this far, everything's valid!
return true;
}
function isURL(urlAddr)
{
alert(urlAddr);
var urlRegxp = /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([\w]+)(.[\w]+){1,2}$/;
var mtch=urlRegxp.test(urlAddr);
return mtch;
}
function isEmpty(strValue)
{
return (strValue.length < 1);
}
function isSelected(optValue)
{
return !(optValue.value == 0);
}
function isAlphaNumeric(strValue)
{ 
var charpos = strValue.search("[^A-Za-z0-9]"); 
return !(strValue.length > 0 && charpos >= 0);
} 
function isValidName(strValue)
{ 
var charpos = strValue.search("[^A-Za-z\.\/ ]"); 
return ! (strValue.length > 0 && charpos >= 0);
}
function isValidLength(strValue, len)
{ 
return (strValue.length == len); 
} 
function isInteger(s)
{
// if(s.length < 1)
// return true;
var i;
for (i = 0; i < s.length; i++)
{ 
// Check that current character is number.
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) 
return false;
}
// All characters are numbers.
return true;
} 
// *** Functions By Naveen G ***//
// /check for alpha only
function isAlpha(strValue)
{ 
var charpos = strValue.search("[^A-Za-z]"); 
if(strValue.length > 0 && charpos >= 0) 
{ 
return false; 
}
return true;
}
// Check decimal with precision "decallowed"
function checkDecimals(fieldValue,decallowed) 
{
if (isNaN(fieldValue) || fieldValue == "") 
return false;
else 
{
if (fieldValue.indexOf('.') == -1) fieldValue += ".";
dectext = fieldValue.substring(fieldValue.indexOf('.')+1, fieldValue.length);
if (dectext.length > decallowed)
return false;
else 
return true;
}
}
function trim(value) 
{
var temp = value;
// alert("Before Trim 1"+temp+"2");
var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
var obj = / /g;
while (temp.match(obj)) { temp = temp.replace(obj, " "); }
// alert("after trim 1"+temp+"2");
return temp;
}
// Genereal template function to handle Validation alert , checks for blank and
// the given function
function fnTemplate(fVarFunction,fVarAlert1,fVarAlert2,fVarControl)
{
if(fVarControl.value == "" || fVarControl.length == 0)
{
alert(fVarAlert1);
fVarControl.focus();
fVarControl.select();
return false;
}
else
if(eval(fVarFunction) == false)
{
alert(fVarAlert2);
fVarControl.focus();
fVarControl.select();
return false;
}
return true;
}
// *** END Naveen G ***//
function checkError(message)
{
if(message.length > 1)
alert(message);
}
var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat');
function _isDate(val,format) {
var date=getDateFromFormat(val,format);
if (date==0) { return false; }
return true;
}
function _isInteger(val) {
var digits="1234567890";
for (var i=0; i < val.length; i++) {
if (digits.indexOf(val.charAt(i))==-1) { return false; }
}
return true;
}
function _getInt(str,i,minlength,maxlength) {
for (var x=maxlength; x>=minlength; x--) {
var token=str.substring(i,i+x);
if (token.length < minlength) { return null; }
if (_isInteger(token)) { return token; }
}
return null;
}
function getDateFromFormat(val,format) {
val=val+"";
format=format+"";
var i_val=0;
var i_format=0;
var c="";
var token="";
var token2="";
var x,y;
var now=new Date();
var year=now.getYear();
var month=now.getMonth()+1;
var date=1;
var hh=now.getHours();
var mm=now.getMinutes();
var ss=now.getSeconds();
var ampm="";
while (i_format < format.length) {
// Get next token from format string
c=format.charAt(i_format);
token="";
while ((format.charAt(i_format)==c) && (i_format < format.length)) {
token += format.charAt(i_format++);
}
// Extract contents of value based on format token
if (token=="yyyy" || token=="yy" || token=="y") {
if (token=="yyyy") { x=4;y=4; }
if (token=="yy") { x=2;y=2; }
if (token=="y") { x=2;y=4; }
year=_getInt(val,i_val,x,y);
if (year==null) { return 0; }
i_val += year.length;
if (year.length==2) {
if (year > 70) { year=1900+(year-0); }
else { year=2000+(year-0); }
}
}
else if (token=="MMM"||token=="NNN"){
month=0;
for (var i=0; i<MONTH_NAMES.length; i++) {
var month_name=MONTH_NAMES[i];
if (val.substring(i_val,i_val+month_name.length).toLowerCase()==month_name.toLowerCase()) {
if (token=="MMM"||(token=="NNN"&&i>11)) {
month=i+1;
if (month>12) { month -= 12; }
i_val += month_name.length;
break;
}
}
}
if ((month < 1)||(month>12)){return 0;}
}
else if (token=="EE"||token=="E"){
for (var i=0; i<DAY_NAMES.length; i++) {
var day_name=DAY_NAMES[i];
if (val.substring(i_val,i_val+day_name.length).toLowerCase()==day_name.toLowerCase()) {
i_val += day_name.length;
break;
}
}
}
else if (token=="MM"||token=="M") {
month=_getInt(val,i_val,token.length,2);
if(month==null||(month<1)||(month>12)){return 0;}
i_val+=month.length;}
else if (token=="dd"||token=="d") {
date=_getInt(val,i_val,token.length,2);
if(date==null||(date<1)||(date>31)){return 0;}
i_val+=date.length;}
else if (token=="hh"||token=="h") {
hh=_getInt(val,i_val,token.length,2);
if(hh==null||(hh<1)||(hh>12)){return 0;}
i_val+=hh.length;}
else if (token=="HH"||token=="H") {
hh=_getInt(val,i_val,token.length,2);
if(hh==null||(hh<0)||(hh>23)){return 0;}
i_val+=hh.length;}
else if (token=="KK"||token=="K") {
hh=_getInt(val,i_val,token.length,2);
if(hh==null||(hh<0)||(hh>11)){return 0;}
i_val+=hh.length;}
else if (token=="kk"||token=="k") {
hh=_getInt(val,i_val,token.length,2);
if(hh==null||(hh<1)||(hh>24)){return 0;}
i_val+=hh.length;hh--;}
else if (token=="mm"||token=="m") {
mm=_getInt(val,i_val,token.length,2);
if(mm==null||(mm<0)||(mm>59)){return 0;}
i_val+=mm.length;}
else if (token=="ss"||token=="s") {
ss=_getInt(val,i_val,token.length,2);
if(ss==null||(ss<0)||(ss>59)){return 0;}
i_val+=ss.length;}
else if (token=="a") {
if (val.substring(i_val,i_val+2).toLowerCase()=="am") {ampm="AM";}
else if (val.substring(i_val,i_val+2).toLowerCase()=="pm") {ampm="PM";}
else {return 0;}
i_val+=2;}
else {
if (val.substring(i_val,i_val+token.length)!=token) {return 0;}
else {i_val+=token.length;}
}
}
// If there are any trailing characters left in the value, it doesn't match
if (i_val != val.length) { return 0; }
// Is date valid for month?
if (month==2) {
// Check for leap year
if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year
if (date > 29){ return 0; }
}
else { if (date > 28) { return 0; } }
}
if ((month==4)||(month==6)||(month==9)||(month==11)) {
if (date > 30) { return 0; }
}
// Correct hours value
if (hh<12 && ampm=="PM") { hh=hh-0+12; }
else if (hh>11 && ampm=="AM") { hh-=12; }
var newdate=new Date(year,month-1,date,hh,mm,ss);
return newdate.getTime();
}
// isDecimal is the function that validate whether a value is decimal or not
function isDecimal(val)
{
p = ',';
value = val.replace(p, '\.');
var value = new Number(value);
return (parseFloat(value))
}
function nextfield(thisform, count, fieldName) 
{
for (i=0; i<document.forms[0].elements.length; i++) 
{
if (document.forms[0].elements[i].name == fieldName) 
{
elementid = i;
break;
}
}
if (document.forms[0].elements[elementid].value.length == count) {
document.forms[0].elements[elementid+1].select();
document.forms[0].elements[elementid+1].focus();
}
}
// To check the entered string is Integer.
var dtCh= "/";
var minYear=1900;
var maxYear=2999;
// **************** Validation of date format starts here *************
function stripCharsInBag(s, bag)
{
var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < s.length; i++)
{ 
var c = s.charAt(i);
if (bag.indexOf(c) == -1)
returnString += c;
}
return returnString;
}
function daysInFebruary (year)
{
// February has 29 days in any year evenly divisible by four,
// EXCEPT for centurial years which are not also divisible by 400.
return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n)
{
for (var i = 1; i <= n; i++)
{
this[i] = 31
if (i==4 || i==6 || i==9 || i==11)
{this[i] = 30}
if (i==2) 
{this[i] = 29}
} 
return this
}
// **************** Validation of date format ends here *************
// ************************* To get the date values to compare
// *********************
function fnGetDateValue(fVarInDate)
{	
var y,d,m,JD=0;	
if(fVarInDate.indexOf("/") == -1)
{
var l = fVarInDate.length, str;
str = fVarInDate.substr(0,2-l%2); if(fnOnlyDigits(str)) d = parseInt(str,10);
str = fVarInDate.substr(2-l%2,2); if(fnOnlyDigits(str)) m = parseInt(str,10);
str = fVarInDate.substr(4-l%2); if(fnOnlyDigits(str)) y = parseInt(str,10);	
}
else
{
var c = fVarInDate.split("/");
if(fnOnlyDigits(c[0])) d = parseInt(c[0],10);
if(fnOnlyDigits(c[1])) m = parseInt(c[1],10);
if(fnOnlyDigits(c[2])) y = parseInt(c[2],10);	
}
MM=eval(m);
DD=eval(d);
YY=eval(y);
with (Math)
{
var S = 0;
GGG = 1;
if (YY <= 1585)
{
GGG = 0;
}
if ((MM - 9)<0)
{	
S=-1;
}	
JD = -1 * floor(7 * (floor((MM + 9) / 12) + YY) / 4);
S = 1;
A = abs(MM - 9);
J1 = floor(YY + S * floor(A / 7));
J1 = -1 * floor((floor(J1 / 100) + 1) * 3 / 4);
JD = JD + floor(275 * MM / 9) + DD + (GGG * J1);
JD = JD + 1721027 + 2 * GGG + 367 * YY;
}	
return JD = JD;	
}
function fnOnlyDigits(str)
{
var re = new RegExp("([0-9]+)");
return (re.exec(str)!=null && RegExp.$1==str);
}
/* Edited by Elizebeth - check text limit for a field */
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length > limitNum) {
limitField.value = limitField.value.substring(0, limitNum);
} else {
limitCount.value = limitNum - limitField.value.length;
}
}
