For each loop to reverse a string (VB.net)
<%@ Page Language="VB" %>
<script runat="server">
Function ValidateCard(cardNumber As String) As Boolean
Dim reverseNumber As String
Dim ch As Char
For Each ch in cardNumber
reverseNumber = ch & reverseNumber
Next
Dim doubledAlternates As String
Dim thisDigit, index As Integer
For index = 1 to reverseNumber.Length
thisDigit = CInt(reverseNumber.SubString(index - 1, 1))
If index mod 2 = 0 Then
thisDigit = thisDigit * 2
End If
doubledAlternates = doubledAlternates & thisDigit
Next
Dim sum As Integer
For index = 1 to doubledAlternates.Length
sum = sum + CInt(doubledAlternates.SubString(index - 1, 1))
Next
ValidateCard = sum <> 0 AND sum mod 10 = 0
End Function
Sub Button1_Click(sender As Object, e As EventArgs)
If ValidateCard(TextBox1.Text) Then
Label1.Text = "That'll do nicely sir!"
Else
Label1.Text = "There's nothing for you here!"
End If
End Sub
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
Enter the number:
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Check Card Number"></asp:Button>
</p>
<p>
<asp:Label id="Label1" runat="server"></asp:Label>
</p>
</form>
</body>
</html>
Related examples in the same category