Wednesday, May 14, 2008

Java Script Validation Functions


/****************************************************/
/* COMMON JAVASCRIPT FUNCTIONS */
/****************************************************/
// VARIABLES FOR COLOR
//var BG_ERR_CLR = 'red'; // error background color.
var FG_ERR_CLR = 'black'; // error foreground color.
var BG_DEF_CLR = 'white'; // default background color.
var FG_DEF_CLR = 'black'; // default foreground color.
var CONTROL = 'CONTROL'; // default foreground color.
var BG_ERR_CLR = '#FFE87C';
//var BG_ERR_CLR = 'RED';
--------------------------Method names --------------------------------------
1. showError()
/**
* showError()
* ================
*
* Show the Message Box (Error, Information, Warning)
*
* Parameters
* ----------
* - MANDATORY MUST BE PASSED BY USER
* - MANDATORY MUST BE PASSED BY USER
* - OPTIONAL
* Return value
* ------------
*/
function showError()
{
lstrMessageID = showError.arguments[0];
lstrMessage = showError.arguments[1];
lobjField = showError.arguments[2];

lstrFinalMsg = lstrMessageID + ' : ' + lstrMessage;

if(lobjField!=null)
{
setErrorColor(lobjField);
lobjField.focus();
}
alert(lstrFinalMsg);
}

2. showQuestion()
/**
* showQuestion()
* ================
*
* Show the Message Box (Warning) - Yes/No
*
* Parameters
* ----------
* - MANDATORY MUST BE PASSED BY USER
* - MANDATORY MUST BE PASSED BY USER
* - OPTIONAL
* Return value
* ------------
* -
*/
function showQuestion()
{
lstrMessageID = showQuestion.arguments[0];
lstrMessage = showQuestion.arguments[1];
lobjField = showQuestion.arguments[2];

lstrFinalMsg = lstrMessageID + ' : ' + lstrMessage;

/*if(lobjField!=null){
lobjField.focus();
}*/
return confirm(lstrFinalMsg);
}

3. getComboUpdated(comboObj,strDispValue, strKey)
// Input Parameter - 1. Combo Object
// 2. Text of the option
// 3. Value that is to be returned, when the option is selected
// Output Parameter - None
// Functionality - This function dynamically adds an option to the combo.

function getComboUpdated(comboObj,strDispValue, strKey)
{
var option = new Option(strDispValue, strKey);
// Add a new option to the combo
var i = comboObj.length;
comboObj.options[i]=option;
}

4. openFlmAsPopup(frameName,url,context1,context2,context3,context4)
// Input Parameter - 1. Url of the popup frame
// 2. Url of screen to be opened in the frame
// 3. object name to be passed to the popup screen.
// 4. object name to be passed to the popup screen.
// 5. object name to be passed to the popup screen.
// 6. object name to be passed to the popup screen.
// Output Parameter - None
// Functionality - This is used to open flm in popup mode..
function openFlmAsPopup(frameName,url,context1,context2,context3,context4)
{
strURL = frameName;
if(context1 != null)
{
strURL = strURL + "&enq_context1=";
strURL = strURL + context1;
}
if(context2 != null)
{
strURL = strURL + "&enq_context2=";
strURL = strURL + context2;
}
if(context3 != null)
{
strURL = strURL + "&enq_context3=";
strURL = strURL + context3;
}
if(context4 != null)
{
strURL = strURL + "&enq_context4=";
strURL = strURL + context4;
}
strURL=strURL + '&url='+escape(url+'&popup=true');
//alert('url constructed is'+strURL);
openPopupWindowWithPos(strURL, '870', '550', '20', '40');

}

5. populateGR(objField, HiddenFieldvalue)
function populateGR(objField, HiddenFieldvalue)
{
if ((objField.value).length == 0)
{
objField.value=HiddenFieldvalue;
}
}

6. RefreshOpenerWindowAndCloseMe(strUrl)
function RefreshOpenerWindowAndCloseMe(strUrl)
{
if(window.opener)
{
// refresh the opener window...as user has selected one of the customer records from the table
window.opener.location.href = strUrl;
// close the current window after refreshing the opener
window.close();
}
}


7. openPopupWindow(strUrl, lngWidth, lngHeight)
function openPopupWindow(strUrl, lngWidth, lngHeight)
{

var strWindowName = "";
var strWindowFeatures;

strWindowFeatures = "toolbar=no,resize=yes,titlebar=no,";
strWindowFeatures = strWindowFeatures + "location=no,directories=no,status=no,scrollbars=yes,"
strWindowFeatures = strWindowFeatures + "menubar=no,width=" + lngWidth + ",height=" + lngHeight + ",maximize=null";

// open the window
window.open(strUrl, "", strWindowFeatures);
}

//--- Enter Numeric value with decimal..
8. function pressCharOnlyHandeler(e)

{

if ((e.keyCode >= 65 && e.keyCode <= 90) || (e.keyCode >= 97 && e.keyCode <= 122) ) {
return true;
}
else
{
return false;
}
}

/**
* isSelected()
* ================
*
* Function to check if any checkbox is selected or not
* Assumes that the checkbox column in the table has been defined by the name 'selected'
*
* Parameters -
* ----------
*
* Return value -
* ------------
*/

9. function isSelected()

{
var table = document.getElementById(arguments[0]);
var numrows = table.rows.length;
if(numrows <= 0) { return false; } var chk = 0; for(i=0;;i++)
{
var str = 'accountDatas[' + i + '].delStatus';
var obj = document.getElementById(str);
if(obj == null)
{
break;
}
if(obj.checked)
{
chk=1; break;
}
}
if(chk==1)
{
return true;
}
else
{
return false;
}
}


/**
* isEmpty()
* ================
*
* Check whether string s is empty.
*
* Parameters
* ----------
* -
*
* Return value
* ------------
* -
*/
10. function isEmpty(lstrValue)

{
lstrValue=leftTrimString(lstrValue);
lstrValue=rightTrimString(lstrValue);

return ((lstrValue == null) (lstrValue.length == 0))
}

/////////////////// STRING FUNCTIONS- BEGIN /////////////////////////////////////

/**
* rightTrimString()
* ================
*
* Trim the blank spaces on the right side of the string
*
* Parameters
* ----------
* -
*
* Return value
* ------------
* -
*/
11. function rightTrimString(astrValue)

{
var lstrValue = astrValue;

lstrValue=lstrValue.replace(/(.*?)\s*$/, "$1");
return lstrValue;
}

/**
* leftTrimString()
* ================
*
* Method to Trim the blank spaces on the left side of the string
*
* Parameters
* ----------
* -
*
* Return value
* ------------
* -
*/
12. function leftTrimString(astrValue)

{
var lstrValue = astrValue;

lstrValue=lstrValue.replace(/^\s*(.*)/, "$1");
return lstrValue;
}

/**
* trimString()
* ================
*
* Method to remove the left and right curtailing spaces
*
* Parameters
* ----------
* -
*
* Return value
* ------------
* -
*/
13. function trimString(astrValue)

{
var lstrValue = astrValue;

lstrValue=leftTrimString(lstrValue);
lstrValue=rightTrimString(lstrValue);
return lstrValue;
}

14. function isInteger(aValue)
{
var lstrValue = aValue;
re = new RegExp(",","g");
lstrValue = lstrValue.replace(re,"");
for (lintCounter = 0; lintCounter < c =" lstrValue.charAt(lintCounter);"> "9"))){
return false;
}
}

/**
* validateFloat()
* ================
*
* To validate that the number passed is valid or not
*
* Parameters
* ----------
* - MANDATORY MUST BE PASSED BY USER
* - MANDATORY MUST BE PASSED BY USER
* - MANDATORY MUST BE PASSED BY USER
* - OPTIONAL (By default it is false)
*
* Return value
* ------------
* -
*/

15. function validateFloat(astrNumber,astrDigitsBefDec,astrDigitsAfterDec)
{
var lreComma = new RegExp(",","g");
var lblnResult = false;
var lstrVal = astrNumber;
var lstrDigitsBefDec = astrDigitsBefDec;
var lstrDigitsAfterDec = astrDigitsAfterDec;
var lblnNegativeAllowed = validateFloat.arguments[3];
// Replace the commas if present in the value
if(lstrVal != null && lstrVal.length > 0){
lstrVal = validateFloat.arguments[0].replace(lreComma,"");
}else{
// If Null is passed then return false
return false;
}

if(lblnNegativeAllowed == null lblnNegativeAllowed.length == 0){
lblnNegativeAllowed = false;
}

if(lblnNegativeAllowed){
//To match positive or negative real numbers with or without a decimal point and 0 to <> decimal places.
lexp = "^-?\\d{0," + lstrDigitsBefDec + "}(\\.\\d{0," + lstrDigitsAfterDec + "})?$";
lreData = new RegExp(lexp);
lblnResult = lreData.test(lstrVal);
}else{
//To match positive real numbers with or without a decimal point and 0 to <> decimal places.
lexp = "^\\d{0," + lstrDigitsBefDec + "}(\\.\\d{0," + lstrDigitsAfterDec + "})?$";
lreData = new RegExp(lexp);
lblnResult = lreData.test(lstrVal);
}
return lblnResult;
}


No comments:

Global Tech Soft Solution

Smart software solution.

Webdesigning, software Development & Consultant GlobalTech Soft Solution