Hi Peter, I have been engrossed in other aspects of the site so have only just got around to following up on this post. First thanks for replying.
Secondly I am not 100% what this code uses, I think I said before I only know VB.NET and not C#, anyway I thought you may be interested to see the code that I am using, this is compiled as a C# component and I just reference it in my code. Its setup seems very similar to that of the Microsoft validators in that you specify the control that it checks and it returns an error message when the validation finds an error.
I have this on a form, as you would, when you click the submit button the form will be submitted and data added to the database but if I press 'Back' (browser button) then it show my data in the form with a message saying that the card is invalid. So I guess it is not stopping the page from submitting.
The component:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CustomValidators
{
/// <summary>
/// Summary description for Class1.
/// </summary>
//-- This is allowing us to inherit the BaseValidator's functionality
public class CreditCardValidator : BaseValidator
{
protected override bool EvaluateIsValid()
{
//-- Set valueToValidate to the validation control's controltovalidate value.
string valueToValidate = this.GetControlValidationValue(this.ControlToValidate);
int indicator = 1; //-- will be indicator for every other number
int firstNumToAdd = 0; //-- will be used to store sum of first set of numbers
int secondNumToAdd = 0; //-- will be used to store second set of numbers
string num1; //-- will be used if every other number added is greater
//-- than 10, store the left-most integer here
string num2; //-- will be used if ever yother number added
//-- is greater than 10, store the right-most integer here
//-- Convert our creditNo string to a char array
char[] ccArr = valueToValidate.ToCharArray();
for (int i=ccArr.Length-1;i>=0;i--)
{
char ccNoAdd = ccArr[i];
int ccAdd = Int32.Parse(ccNoAdd.ToString());
if (indicator == 1)
{
//-- If we are on the odd number of numbers, add that number to our total
firstNumToAdd += ccAdd;
//-- set our indicator to 0 so that our code will know to skip to the next piece
indicator = 0;
}
else
{
//-- if the current integer doubled is greater than 10
//-- split the sum in to two integers and add them together
//-- we then add it to our total here
if ((ccAdd + ccAdd) >= 10)
{
int temporary = (ccAdd + ccAdd);
num1 = temporary.ToString().Substring(0,1);
num2 = temporary.ToString().Substring(1,1);
secondNumToAdd += (Convert.ToInt32(num1) + Convert.ToInt32(num2));
}
else
{
//-- otherwise, just add them together and add them to our total
secondNumToAdd += ccAdd + ccAdd;
}
//-- set our indicator to 1 so for the next integer
//-- we will perform a different set of code
indicator = 1;
}
}
//-- If the sum of our 2 numbers is divisible by 10,
//-- then the card is valid. Otherwise, it is not
bool isValid = false;
if ((firstNumToAdd + secondNumToAdd) % 10 == 0)
{
isValid = true;
}
else
{
isValid = false;
}
return isValid;
}
}
}
Thank you once again for your help.
iM
Thanks,
iMonkey