Friday, September 14, 2012

Javascript for blocking numbers

<script language="javascript" type="text/javascript">
function blockNonNumbers(obj, e, allowDecimal, allowNegative) {

var key;
var isCtrl = false;
var keychar;
var reg;
if (window.event) {key = e.keyCode;
isCtrl = window.event.ctrlKey
}

else if (e.which) {key = e.which;
isCtrl = e.ctrlKey;
}

if (isNaN(key)) return true;keychar = String.fromCharCode(key);

// check for backspace or delete, or if Ctrl was pressedif (key == 8 || isCtrl) {
return true;}
reg = /\d/;

var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
return isFirstN || isFirstD || reg.test(keychar);}
</script>
<div class="floatl width320"><div class=" left_part">effective from:<span class="colorred">*</span></div><div class="right_part"><asp:TextBox ID="txtEffectiveFrom" runat="server" class="textbox"></asp:TextBox><img alt="Calendar" src='<%=Page.ResolveClientUrl("App_Themes/images/meal_booking_datepicker.png") %>'onclick="javascript:displayCalendar(document.getElementById('<%=txtEffectiveFrom.ClientID%>'),'mm/dd/yyyy',this);"id="imgfrom" class="margintop-3" /></div></div>

Javascript For trimming the whitespace and to check the white spaces

Below script is to validating the blank fields:
<script language="javascript" type="text/javascript"
>function isBlank(s) {
if (s == null)
return true;
if (s.match(/^\s*$/)) {
return true}return false;}

function trim(str, chars) {
return ltrim(rtrim(str, chars), chars);}

function ltrim(str, chars) {chars = chars ||
"\\s";
return str.replace(new RegExp("^[" + chars + "]+", "g"), "");}

function rtrim(str, chars) {chars = chars ||
"\\s";
return str.replace(new RegExp("[" + chars + "]+$", "g"), "");}

</script>

e.g:
if (isBlank(trim(objPrice.value, ' ')) || trim(objPrice.value, ' ').length == 0) {
alert(
"Please enter Price.");document.getElementById(price).focus();

return false;}

Wednesday, September 12, 2012

Move Values from one list box to another

<script language="javascript" type="text/javascript">
function listbox_moveacross(sourceID, destID) {
var str = '';
var src = document.getElementById(sourceID);
var dest = document.getElementById(destID);
var j = 0;
for (var count = 0; count < src.options.length; count++) {
if (src.options[count].selected == true) {j++;
}
}

if (j > 2) {alert(
"Maximum 2 Week Of Day you can select")
return false;}

else {
for (var count = 0; count < src.options.length; count++) {
if (src.options[count].selected == true) {
var option = src.options[count];
var newOption = document.createElement("option");newOption.value = option.value;
newOption.text = option.text;
newOption.selected =
true;
if (str.length == 0) {str = option.value;
}

else {str = str +
"," + option.value;}

try {dest.add(newOption,
null); //Standardsrc.remove(count, null);}
catch (error) {dest.add(newOption);
// IE onlysrc.remove(count);
}
count--;
}
}
str =
'';
var list = document.getElementById('<%=lstWeekOffDays.ClientID %>');
for (var count = 0; count < list.options.length; count++) {
var option = list.options[count];
var newOption = document.createElement("option");newOption.value = option.value;
newOption.text = option.text;
newOption.selected =
true;
if (str.length == 0) {str = option.value;
}

else {str = str +
"," + option.value;}
}
document.getElementById(
'<%=hdnWeekOffDays.ClientID %>').value = str; }
}

</script>
<table><tr><td><asp:ListBox ID="lstWeekDays" SelectionMode="Multiple" runat="server"></asp:ListBox></td><td><table><tr><td>
<input type="button" class="btnHyperlink" id="HyLeft" value=">>" onclick="listbox_moveacross('<%=lstWeekDays.ClientID %>','<%=lstWeekOffDays.ClientID %>')"; /> 
</td></tr><tr><td><input type="button" class="btnHyperlink" id="HyRight" value="<<" onclick="listbox_moveacross('<%=lstWeekOffDays.ClientID %>','<%=lstWeekDays.ClientID %>')"; /></td></tr></table></td><td><asp:ListBox ID="lstWeekOffDays" SelectionMode="Multiple" runat="server"></asp:ListBox></td></tr><tr><td><asp:Button runat="server" ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" /></td></tr><asp:HiddenField ID="hdnWeekOffDays" runat="server" /></table>