Tuesday, December 12, 2006

Function which returns the "Selected Texts" of a multiselect listbox

Function ReturnSelectedTexts(ByVal objControlName As Object) As String
'This function returns the selected texts of the multiselect listbox
Dim strSelectedTexts As String
Dim lstItem As ListItem
For Each lstItem In objControlName.items
If lstItem.Selected = True Then
strSelectedTexts = strSelectedTexts & lstItem.Text & ", "
End If
Next
strSelectedTexts = Trim(strSelectedTexts)
strSelectedTexts = Left(strSelectedTexts, Len(strSelectedTexts) - 1)
Return strSelectedTexts
End Function

'----Usage
lblSearchCriterias.Text = "Email verified: " & ReturnSelectedTexts(lstEmailVerified)

'--It will give the selected items as comma separated.

Cheers
Rajesh Sivaraman.

Sunday, December 10, 2006

Disallow some characters in a string...

function disallowCharacters(strText)
{
//The parameter to the function is the string which is to be validated
//The variable "strNotAllowed" below this line should contain the characters
// which are not allowed in the string
var strNotAllowed = ";:/\,&\\";
var checkStr = strText;
var allValid = true;
for (i = 0; i < strNotAllowed.length; i++)
{
if (strText.indexOf(strNotAllowed.charAt(i)) > 0)
{
allValid = false;
}
}
return allValid;
}

//----------------------------------
//how to use

if (document.Form1.txtemail.value!="")
{
//alert(disallowCharacters(document.Form1.txtemail.value));
if (!disallowCharacters(document.Form1.txtemail.value))
{
//alert('Invalid Character');
alert("Please enter a valid Email Address");
document.Form1.txtemail.focus();
return false;
}
else
{
bemail1=new RegExp(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/);
bemail2 = bemail1.exec(document.Form1.txtemail.value);

if(!bemail2)
{
alert("Please enter a valid Email Address");
document.Form1.txtemail.focus();
return false;
}
}
}

Function which allows only integers...

function IsInteger(varInt)
{
var checkOK = "0123456789";
var checkStr = varInt;
var allValid = true;
var allNum = "";
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch != ",")
allNum += ch;
}
return allValid;
}

//--------------------

//how to use

if (document.Form1.txtMainTelephone.value!="")
{
if (!IsInteger(document.Form1.txtMainTelephone.value))
{
alert("Invalid character entered in main telephone number!\nPlease enter only numbers.");
document.Form1.txtMainTelephone.focus();
return false;
}
}