Saturday, August 1, 2009

Validating Credit card

Use the following javascript function to validate a given credit card number on client side

function Mod10(ccNumb) { // v2.0
var valid = "0123456789" // Valid digits in a credit card number
var len = ccNumb.length; // The length of the submitted cc number
var iCCN = parseInt(ccNumb); // integer of ccNumb
var sCCN = ccNumb.toString(); // string of ccNumb
sCCN = sCCN.replace (/^\s+|\s+$/g,''); // strip spaces
var iTotal = 0; // integer total set at zero
var bNum = true; // by default assume it is a number
var bResult = false; // by default assume it is NOT a valid cc
var temp; // temp variable for parsing string
var calc; // used for calculation of each digit

// Determine if the ccNumb is in fact all numbers
for (var j=0; j<.len; j++) {
temp = "" + sCCN.substring(j, j+1);
if (valid.indexOf(temp) == "-1"){bNum = false;}
}

// if it is NOT a number, you can either alert to the fact, or just pass a failure
if(!bNum){
/*alert("Not a Number");*/bResult = false;
}

// Determine if it is the proper length
if((len == 0)&&(bResult)){ // nothing, field is blank AND passed above # check
bResult = false;
} else{ // ccNumb is a number and the proper length - let's see if it is a valid card number
if(len >= 15){ // 15 or 16 for Amex or V/MC
for(var i=len;i>0;i--){ // LOOP throught the digits of the card
calc = parseInt(iCCN) % 10; // right most digit
calc = parseInt(calc); // assure it is an integer
iTotal += calc; // running total of the card number as we loop - Do Nothing to first digit
i--; // decrement the count - move to the next digit in the card
iCCN = iCCN / 10; // subtracts right most digit from ccNumb
calc = parseInt(iCCN) % 10 ; // NEXT right most digit
calc = calc *2; // multiply the digit by two
// Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
// I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
switch(calc){
case 10: calc = 1; break; //5*2=10 & 1+0 = 1
case 12: calc = 3; break; //6*2=12 & 1+2 = 3
case 14: calc = 5; break; //7*2=14 & 1+4 = 5
case 16: calc = 7; break; //8*2=16 & 1+6 = 7
case 18: calc = 9; break; //9*2=18 & 1+8 = 9
default: calc = calc; //4*2= 8 & 8 = 8 -same for all lower numbers
}
iCCN = iCCN / 10; // subtracts right most digit from ccNum
iTotal += calc; // running total of the card number as we loop
} // END OF LOOP

if ((iTotal%10)==0){ // check to see if the sum Mod 10 is zero
bResult = true; // This IS (or could be) a valid credit card number.
} else {
bResult = false; // This could NOT be a valid credit card number
}
}
}
// change alert to on-page display or other indication as needed.
if(bResult) {
alert("This IS a valid Credit Card Number!");
}
if(!bResult){
alert("This is NOT a valid Credit Card Number!");
}
return bResult; // Return the results
}



CAll this function on button click

Monday, February 9, 2009

Getting the list of files dragged on a control

There may be some situations in which there is a picture box sort of control and u r expected to drag image files on it to open them directly . To do so,we must deal with Drag Enter event.

In Drag Enter event just add the following code:

string[] Files = (string[])e.Data.GetData(DataFormats.FileDrop, false);

Where 'e' is the DragEventArg

The string array Files[] will contain the paths of the files dragged on to the control. After getting the files paths u can directly use them to get the desired functionality.

Friday, February 6, 2009

IMAGE SLIDING USING AJAX & JAVA SCRIPT

Take one Image and two Ajax Slider extender controls and set TargetControlID and BoundControlID' textboxes.

The design script code looks like (Note: As all you smart people know that tags are not allowed in blogspot, the starting and ending tags are replaced by '(' , ')' correspondingly)

(img id="imgZoomTarget" src="images/no daughter.bmp"

style="position: absolute;top: 92px; left: 378px; height: 343px; width: 308px" /)

(form id="form1" runat="server")

(asp:ScriptManager ID="ScriptManager1" runat="server" /)

(asp:TextBox ID="Slider2" runat="server")(/asp:TextBox)

(asp:TextBox ID="Slider2_BoundControl" runat="server")(/asp:TextBox)

(asp:TextBox ID="Slider3" runat="server")(/asp:TextBox)

(asp:TextBox ID="Slider3_BoundControl" runat="server")(/asp:TextBox)

(cc1:SliderExtender ID="heightSlider" runat="server" BehaviorID="Slider2"

TargetControlID="Slider2" BoundControlID="Slider2_BoundControl" Orientation="Vertical"

EnableHandleAnimation="true" Minimum="10" /)

(cc1:SliderExtender ID="widthSlider" runat="server" BehaviorID="Slider3"

TargetControlID="Slider3" BoundControlID="Slider3_BoundControl" Orientation="Horizontal"

EnableHandleAnimation="true" Minimum="10" /)


And now add the following java script code in body tag:


var HundredPercentHeight = 800;

var HundredPercentWidth = 800;

var controlID ="imgZoomTarget";

//Function to Modify Height according to Slider Value

function ZoomControlHeight(eventArgs)

{

var currentlyZoomingControl =$get(controlID);

//Calculate the new height

var newHeight = HundredPercentHeight * ( parseInt( eventArgs.get_Value() ) / 100 ) ;

//Assign the New Height

currentlyZoomingControl.style.height = parseInt(newHeight) +"px";

//Let go of the Control Reference

currentlyZoomingControl = null;

}

function ZoomControlWidth(eventArgs)

{

var currentlyZoomingControl =$get(controlID);

if( eventArgs.get_Value() == null)

return;

var newWidth = HundredPercentWidth * ( parseInt( eventArgs.get_Value() ) / 100 ) ;

currentlyZoomingControl.style.width = parseInt(newWidth) +"px";

currentlyZoomingControl = null;

}

function pageLoad(sender,e)

{

var sliderbehaviour = $find('Slider3');

sliderbehaviour.add_valueChanged(ZoomControlWidth);

sliderbehaviour = $find('Slider2');

sliderbehaviour.add_valueChanged(ZoomControlHeight);

sliderbehaviour = null;

}