279 lines
9.1 KiB
JavaScript
279 lines
9.1 KiB
JavaScript
var dtCh= "/";
|
|
var minYear=1900;
|
|
var maxYear=2100;
|
|
var invalidFields=0;
|
|
//RowDelim="~R0W~";
|
|
//ColDelim="~C0L~";
|
|
|
|
function validate(el,isRequired,ValidFormat)
|
|
{
|
|
flag=true;
|
|
if((el.value=='' || ((el.value.search('-1')>-1) && el.type=='select-one')) && isRequired=='t')
|
|
{
|
|
flag=false;
|
|
}
|
|
if(el.value!='' && ValidFormat!='')
|
|
{
|
|
if(ValidFormat=='Date'){flag=isDate(el.value);}
|
|
else
|
|
{
|
|
var re = new RegExp(GetRegExp(el,ValidFormat));
|
|
var m = re.exec(el.value);
|
|
if (m == null) {
|
|
flag=false;
|
|
} else {
|
|
flag=true;
|
|
}
|
|
}
|
|
}
|
|
if (flag)
|
|
{
|
|
if(el.type=='text' || el.type=='textarea' || el.type=='password')
|
|
{
|
|
el.setAttribute("class", "textbox");
|
|
}
|
|
else if(el.type=='select-one')
|
|
{
|
|
el.parentNode.setAttribute("class", "divselect");
|
|
}
|
|
if (document.getElementById('err'+el.id))
|
|
{
|
|
invalidFields=invalidFields==0 ? 0:invalidFields-1;
|
|
document.getElementById('err'+el.id).parentNode.removeChild(document.getElementById('err'+el.id));
|
|
}
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
highlightField(el);
|
|
return false;
|
|
}
|
|
}
|
|
function GetRegExp(el,ValidFormat)
|
|
{
|
|
|
|
if(ValidFormat=='Email'){return /^\S*\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,8}\b\S*$/;}
|
|
if(ValidFormat=='Numeric'){return /^\d*$/;}
|
|
if(ValidFormat=='AlphaNumeric'){return /^[a-zA-Z0-9_\*\/\-]*$/;}
|
|
if(ValidFormat=='AlphaNumericOnly'){return /^[a-zA-Z0-9]*$/;}
|
|
if(ValidFormat=='Key'){return /^[a-zA-Z0-9=]*$/;}
|
|
if(ValidFormat=='AlphaSpace'){return /^[a-zA-Z0-9()\/ &]*$/;}
|
|
if(ValidFormat=='AlphaOnly'){return /^[a-zA-Z]*$/;}
|
|
if(ValidFormat=='Alpha'){return /^[a-zA-Z\/]*$/;}
|
|
if(ValidFormat=='LoanAmount'){return /^[a-zA-Z0-9. ]*$/;}
|
|
if(ValidFormat=='MobileNo'){return /^\d{10}([\/]\d{10}){0,1}$/;}
|
|
if(ValidFormat=='Mobile'){return /^\d{10}$/;}
|
|
if(ValidFormat=='Mobilenos'){return /^\d{10}([,|;]\d{10})*$/;}
|
|
if(ValidFormat=='PersonName'){return /^[a-zA-Z ]+$/;}
|
|
if(ValidFormat=='Other'){return /^[a-zA-Z0-9()#@\-]*$/;}
|
|
if(ValidFormat=='SplInstruction'){return /^[a-zA-Z0-9():,#@\.\-\/ ]*$/;}
|
|
if(ValidFormat=='Remarks'){return /^[a-zA-Z0-9,(),:+&#@\'\.\-\/ ]*$/;}
|
|
if(ValidFormat=='Pincode'){return /^\d{6}$/;}
|
|
if(ValidFormat=='Phone'){return /^((\d{6,8}|\d{10}|\d{3}-\d{6,8})([\/](\d{6,8}|\d{10}|\d{3}-\d{6,8})){0,2})$/;}
|
|
if(ValidFormat=='Extension'){return /^\d{3,4}$/;}
|
|
if(ValidFormat=='Time'){return /^(0?[1-9]|1[012])(:[0-5]\d)$/ ;}
|
|
if(ValidFormat=='TimeMS'){return /^[0-2]?[0-9]:[0-5][0-9]:[0-5][0-9]$/ ;}
|
|
if(ValidFormat=='DateTime'){return /^(([0-2]?\d)|([3][01]))\/[0,1]?\d\/((199\d)|([2-9]\d{3}))\s[0-2]?[0-9]:[0-5][0-9]:[0-5][0-9]$/ ;}
|
|
if(ValidFormat=='Date'){return /^(([0-2]?\d)|([3][01]))\/[0,1]?\d\/((199\d)|([2-9]\d{3}))$/ ;}
|
|
if(ValidFormat=='MultipleDates'){return /^(([0-2]?\d)|([3][01]))\/[0,1]?\d\/((199\d)|([2-9]\d{3}))([,](([0-2]?\d)|([3][01]))\/[0,1]?\d\/((199\d)|([2-9]\d{3}))){0,5}$/ ;}
|
|
//if(ValidFormat=='DateTime'){return /^[0,1]?\d\/(([0-2]?\d)|([3][01]))\/((199\d)|([2-9]\d{3}))\s[0-2]?[0-9]:[0-5][0-9]:[0-5][0-9]$/ ;}
|
|
if(ValidFormat=='SMS'){return /^[a-zA-Z0-9\.\/_ ,!&;:()@\-]*$/;}
|
|
if(ValidFormat=='AssmtYear'){return /^((\d{4}-\d{2})|(\d{4} - \d{2}))$/;}
|
|
if(ValidFormat=='PAN'){return /^(([a-zA-Z]{5})\d{4}([a-zA-Z]{1}))$/;}
|
|
if(ValidFormat=='CreditCard'){return /^\d{16}$/;}
|
|
}
|
|
function highlightField(el)
|
|
{
|
|
if (document.getElementById('err'+el.id))
|
|
{
|
|
//nothing to do already exists
|
|
}
|
|
else
|
|
{
|
|
invalidFields=invalidFields+1;
|
|
var iconHost=el.parentNode;
|
|
var current=el.parentNode;
|
|
while(current && current!==document.body)
|
|
{
|
|
var classes=" "+(current.className || "")+" ";
|
|
if(classes.indexOf(" inputcontainer ")!==-1)
|
|
{
|
|
iconHost=current;
|
|
break;
|
|
}
|
|
current=current.parentNode;
|
|
}
|
|
if(el.type=='text' || el.type=='textarea' || el.type=='password')
|
|
{
|
|
el.setAttribute("class", "textbox errtxt");
|
|
}
|
|
else if(el.type=='select-one')
|
|
{
|
|
el.parentNode.setAttribute("class", "divselect errdiv");
|
|
}
|
|
iconHost.style.position="relative";
|
|
var img=document.createElement("img");
|
|
img.setAttribute("src","/matrix/images/exclamation.png");
|
|
img.setAttribute("class", "matrix-validation-error-icon"+(el.type=='select-one' ? " matrix-validation-error-icon--select" : ""));
|
|
img.setAttribute("style","position:absolute;right:"+(el.type=='select-one' ? "24px" : "5px")+";top:50%;width:14px;height:14px;transform:translateY(-50%);z-index:3;pointer-events:none");
|
|
img.setAttribute("id","err"+el.id);
|
|
iconHost.appendChild(img);
|
|
}
|
|
}
|
|
|
|
// DATE FORMAT VALIDATOR DD/MM/YYYY
|
|
function addSlashes(el,evt)
|
|
{
|
|
if (!evt) return false;
|
|
var charCode = evt.which || evt.keyCode;
|
|
var s=el.value;
|
|
if(charCode==8 || charCode==9 || charCode==37 || charCode==38 || charCode==39 || charCode==40 || charCode==46 || charCode==110 || charCode==16 || charCode==36 || charCode==35)
|
|
{
|
|
return true;
|
|
}
|
|
if((charCode>=48 && charCode<=57)||(charCode>=96 && charCode<=105))
|
|
{
|
|
if(s.length==2 || s.length==5)
|
|
{
|
|
el.value=s+"/";
|
|
}
|
|
else if(s.length==10)
|
|
{
|
|
el.value=s+" ";
|
|
}
|
|
else if(s.length==13 || s.length==16)
|
|
{
|
|
el.value=s+":";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
function isInteger(s){
|
|
var i;
|
|
for (i = 0; i < s.length; i++){
|
|
var c = s.charAt(i);
|
|
if (((c < "0") || (c > "9"))) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function stripCharsInBag(s, bag){
|
|
var i;
|
|
var 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){
|
|
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;
|
|
}
|
|
|
|
function isDate(dtStr){
|
|
var daysInMonth = DaysArray(12);
|
|
var pos1=dtStr.indexOf(dtCh);
|
|
var pos2=dtStr.indexOf(dtCh,pos1+1);
|
|
var strDay=dtStr.substring(0,pos1);
|
|
var strMonth=dtStr.substring(pos1+1,pos2);
|
|
var strYear=dtStr.substring(pos2+1);
|
|
strYr=strYear;
|
|
if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
|
|
if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
|
|
for (var i = 1; i <= 3; i++) {
|
|
if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
|
|
}
|
|
month=parseInt(strMonth);
|
|
day=parseInt(strDay);
|
|
year=parseInt(strYr);
|
|
if (pos1==-1 || pos2==-1){
|
|
errmsg="The date format should be : dd/mm/yyyy";
|
|
return false;
|
|
}
|
|
if (strMonth.length<1 || month<1 || month>12){
|
|
errmsg="Please enter a valid month";
|
|
return false;
|
|
}
|
|
if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
|
|
errmsg="Please enter a valid day";
|
|
return false;
|
|
}
|
|
if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
|
|
errmsg="Please enter a valid 4 digit year between "+minYear+" and "+maxYear;
|
|
return false;
|
|
}
|
|
if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
|
|
errmsg="Please enter a valid date";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
function NumberOnly(event) {
|
|
var evt = event;
|
|
var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
|
|
if ((charCode >= 48 && charCode <= 57) || (charCode == 9 || charCode==13 || charCode==8 || charCode==37 || charCode==39 || charCode==46)) {
|
|
return true;
|
|
}
|
|
else {
|
|
return false;
|
|
}
|
|
}
|
|
function FormatTime(event) {
|
|
var evt = event;
|
|
var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
|
|
if ((charCode >= 48 && charCode <= 57) || (charCode == 9 || charCode==13 || charCode==8 || charCode==37 || charCode==39)) {
|
|
return true;
|
|
}
|
|
else {
|
|
return false;
|
|
}
|
|
}
|
|
function ActLikeCheckbox(event,el) {
|
|
var evt = event;
|
|
var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
|
|
if ((charCode >= 48 && charCode <= 57) || charCode==13 || (charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122) || (charCode == 32)) {
|
|
el.value=(el.value=='X') ? '':'X';
|
|
return false;
|
|
}
|
|
else if (charCode == 9 || charCode==8 || charCode==37 || charCode==39)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
function ZeroIfBlank(el)
|
|
{
|
|
if(el.value.trim()=='')
|
|
{
|
|
el.value='0';
|
|
}
|
|
}
|
|
function removeErrorMarks()
|
|
{
|
|
var errcode='err',str='';
|
|
$("[id*="+errcode+"]").each(function(){
|
|
str=(this.id).replace('err','');
|
|
validate(document.getElementById(str),'f','');
|
|
});
|
|
}
|
|
|
|
function toggleErrorMarks(obj,errflag,validFormat)
|
|
{
|
|
validate(obj,errflag,validFormat);
|
|
}
|