Hi nedash,
Refer below code.
HTML
<asp:Label ID="lblID" runat="server" />
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
generateStopSpamText();
}
private void generateStopSpamText()
{
string firstNumber = GenerateRandomPersianNumber();
System.Threading.Thread.Sleep(100);
string secondNumber = GenerateRandomPersianNumber();
ViewState["spam"] = Convert.ToInt16(PersianToEnglish(firstNumber)) + Convert.ToInt16(PersianToEnglish(secondNumber));
lblID.Text = firstNumber + " + " + secondNumber;
}
private string[] persion = { "۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹" };
private string[] english = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
public string PersianToEnglish(string strNum)
{
string chash = strNum;
for (int i = 0; i < 10; i++)
{
chash = chash.Replace(persion[i], english[i]);
}
return chash;
}
protected string GenerateRandomPersianNumber()
{
string numbers = "۱۲۳۴۵۶۷۸۹";
string characters = numbers;
string id = string.Empty;
for (int i = 0; i < 1; i++)
{
string character = string.Empty;
do
{
int index = new Random().Next(0, characters.Length);
character = characters.ToCharArray()[index].ToString();
} while (id.IndexOf(character) != -1);
id += character;
}
return id;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
generateStopSpamText()
End Sub
Private Sub generateStopSpamText()
Dim firstNumber As String = GenerateRandomPersianNumber()
Threading.Thread.Sleep(100)
Dim secondNumber As String = GenerateRandomPersianNumber()
ViewState("spam") = Convert.ToInt16(PersianToEnglish(firstNumber)) + Convert.ToInt16(PersianToEnglish(secondNumber))
lblID.Text = firstNumber & " + " & secondNumber
End Sub
Private persion As String() = {"۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"}
Private english As String() = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
Public Function PersianToEnglish(ByVal strNum As String) As String
Dim chash As String = strNum
For i As Integer = 0 To 10 - 1
chash = chash.Replace(persion(i), english(i))
Next
Return chash
End Function
Protected Function GenerateRandomPersianNumber() As String
Dim numbers As String = "۱۲۳۴۵۶۷۸۹"
Dim characters As String = numbers
Dim id As String = String.Empty
For i As Integer = 0 To 0
Dim character As String = String.Empty
Do
Dim index As Integer = New Random().Next(0, characters.Length)
character = characters.ToCharArray()(index).ToString()
Loop While id.IndexOf(character) <> -1
id += character
Next
Return id
End Function