PDA

View Full Version : How to put condition



user_E
Apr 4th, 2007, 07:51 AM
Hello All,

I have written a common function for validating fields in .net project.
now in function there are two parameters
1) fields not required to validate
2) Group name i.e if it is passed then validation of object will be done which comes under that group name.

Problem is if i pass group name then i want to validate controls which are in that group but if i dont pass group i want to validate all controls throughout but i dont want to exeed size of my JS .


Now folllowing is code :

Code in blue is common which i want to execute in both cases.
Code which is in purple i know i have play with condition in there but i couldnot fix the problem..
which condition i should put so i dont have to repeat code in blue in else condition.


function IsInvalidChrs(FldsNotToValidate,Grpname)
{ alert(Grpname)
FldsNotToValidate = FldsNotToValidate == void 0 ? "" : FldsNotToValidate;
Grpname = Grpname == void 0 ? "" : Grpname;
var strChr = "~,^,'";
var FldIds = ","+FldsNotToValidate;
var strSplit = strChr.split(",");
var elem = document.forms[0].elements;
var blnvar =false;
if(Grpname != "")
{ blnvar = true; }
try
{ for(var i=0; i<elem.length; i++)
{ var objtxt = elem[i];
var Grpnm=objtxt.attributes["validationgrp"];

if(blnvar == true )
{ if(Grpnm)
{ strGrp = Grpnm.value
if (Grpname == strGrp) { if(objtxt.type == 'text' || objtxt.type == 'textarea')
{ var strPos1 = FldIds.indexOf((","+objtxt.id));
if(strPos1 < 0)
{ var objVal = objtxt.value;
for(j=0; j<strSplit.length; j++)
{
var strPos = objVal.indexOf(strSplit[j]);
if(strPos >= 0)
{
alert("Special characters(~,^,') not allowed")
objtxt.focus();
return true;
}
}
// For Validation of Mandatory Fields
var AttVal = objtxt.attributes["validationtype"];
var AttMsg = objtxt.attributes["validationmsg"];
var AttMsg1 = "";
if(AttVal)
{
if(AttMsg) { AttMsg1 = AttMsg.value; }

var AttVal1 = AttVal.value
var strPos2 = AttVal1.indexOf("mandatory")
if(strPos2 >= 0)
{
if(Trim(objtxt.value) == '')
{
if(AttMsg1 != "") { ShowMsg(AttMsg1); } else { alert('This field cannot be Empty'); }
objtxt.focus();
return true;
}
}

// For Validation of Numeric
strPos2 = AttVal1.indexOf("numeric")
if(strPos2 >= 0)
{
var objVal = objtxt.value;
objVal = Trim(objVal);
if(objVal != '')
{
if(isNaN(objVal))
{
if(AttMsg1 != '') { ShowMsg(AttMsg1); } else { alert('Please enter Numeric Values'); }
objtxt.focus();
return true;
}
}
}
}
}
}
else if(objtxt.type == 'select-one')
{
var objVal = objtxt.value;
// For Validation of Mandatory Fields
var AttVal = objtxt.attributes["validationtype"];
var AttMsg = objtxt.attributes["validationmsg"];
var AttMsg1 = "";
if(AttVal)
{
if(AttMsg) { AttMsg1 = AttMsg.value; }

var AttVal1 = AttVal.value
var strPos2 = AttVal1.indexOf("mandatory")
if(strPos2 >= 0)
{
if(Trim(objVal) == '' || objVal.toUpperCase() == '--SELECT--' || objtxt.selectedIndex ==-1 || objVal.toUpperCase() == '--SELECT--^--SELECT--')
{
if(AttMsg1 != "") { ShowMsg(AttMsg1); } else { alert('This field cannot be Empty'); }
objtxt.focus();
return true;
}
}
}
}
} }
}
}
return false;
}
catch(er)
{
alert(er.description);
return true;
}
}


HOPE u people understand the problem..........:o