Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here's my aspx code:
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>


<!DOCTYPE html>
<html>
<head>
<title>Registration</title>
<meta style="zoom: inherit; fit: initial" name="viewport" content="width=device-width,initial-scale=0.7" charset="UTF-8">
<link type="text/css" rel="stylesheet" href="Scripts/styles/bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="Scripts/styles/bootstrap-theme.min.css" />
</head>
<script type="text/javascript" src="jQuery/jquery-1.8.2.js"></script>
<script type="text/javascript" src="Scripts/bootstrap.min.js"></script>
<script type="text/javascript">

$(function () {

$('#txtPhone').keydown(function (e) {

if (e.shiftKey || e.ctrlKey || e.altKey) {

e.preventDefault();

} else {

var key = e.keyCode;
if ((key == 64) || (key == 35) || (key == 42))
e.preventDefault();
if (!((key == 8) || (key == 46) || (key >= 35 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105))) {

e.preventDefault();



}

}

});

});

</script>
<script type="text/javascript">

$(function () {

$('#txtName').keydown(function (e) {

if (e.ctrlKey || e.altKey) {

e.preventDefault();

} else {

var key = e.keyCode;
if (key == 64)
e.preventDefault();
if (!((key > 64 && key < 91) || (key > 96 && key < 123) || (key == 32) || (key == 8))) {

e.preventDefault();

}

}

});

});

</script>
<script type="text/javascript">
function changeSize() {
// change or not?
var index = document.getElementById('ddlCountry').selectedIndex;
var change = document.getElementById('ddlCountry').options[index].text;

if (index == "0")
document.getElementById('txtPhone').size = "10";
else
document.getElementById('txtPhone').size = "15";
}
</script>


<body>
<form id="mainform" runat="server">
<header>

<img src="images/annual_logo.png" />
<img src="images/wifi_logo.png" />

</header>



<!--Mobile Num!-->

<!--Country!-->
<!-- Code!-->
<tr id="trLabel" runat="server" visible="false">
Welcome
<asp:Label runat="server" ID="lblName" Text="Name">
<input style="width: 270px; border-width: 2px; border-color: orange" name="Name" type="text" id="txtName" />

<%--<asp:TextBox onDrop="blur();return false;" type="text" oncopy="return false" onpaste="return false"
oncut="return false" MaxLength="200" ID="txtName" runat="server" Width="270px"
BorderColor="Orange" BorderWidth="2px">


--%>
<asp:Label runat="server" ID="Label1" Text="Country">
<%-- <asp:DropDownList runat="server" Width="270px" ID="ddlCountry" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged"
AutoPostBack="True">
--%>
<select ></select>
<%--<datalist id="ddlCountry" runat="server" >--%>
<asp:Label runat="server" ID="Label3" Text="Code">
<input type="hidden" id="lblCode" runat="server" />
<asp:Label runat="server" ID="Label2" Text="Mobile No.">
<input form="mainform" name="txtPhone" type="tel" id="txtPhone" />
<input src="images/submit.jpg" alt="Submit" runat="server" id="Image1" />










<img src="images/footer_logo1.png" />
<img src="images/docomo_logo.png" />


</form>
</body>
</html>


Here's the cs file code:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page
{
private SqlConnection sqlConnection;
private SqlDataAdapter dbAdapter;
private SqlCommand cmd = new SqlCommand();
string IPAdress = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
try
{
string ph = Request.Form["Phone"];
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
Response.CacheControl = "Public";

//trLabel.Visible = false;
if (!IsPostBack)
{

DataSet DsCountryDetails = new DataSet();
DsCountryDetails = getCountryDetails();
if (DsCountryDetails != null && DsCountryDetails.Tables[0].Rows.Count > 0)
{
ddlCountry.DataSource = DsCountryDetails;
ddlCountry.DataTextField = "code";
ddlCountry.DataValueField = "CountryCode";
ddlCountry.DataBind();
}
}
if (ddlCountry.Value == "91")
{

}
else
{
//ph.Length.Equals(15);
// txtphone.maxlength = 15;
}
}
catch (Exception ex)
{
throw ex;
}
}

protected void Image1Click(object sender, EventArgs e)
{
try
{
string UserAgent = string.Empty;
UserAgent = HttpContext.Current.Request.UserAgent.ToLower();

string nm = Request.Form["Name"];
string ph = Request.Form["txtPhone"];

if (nm == "" || ph == "" || ddlCountry.DataTextField == "Select")
{
Response.Write("<script language='javascript'>alert('Please check whether you have filled in NAME and MOBILE NO fields properly!');</script>");
return;
}
if (ddlCountry.Value == "91")
{
if (!ph.StartsWith("7") && !ph.StartsWith("8") && !ph.StartsWith("9"))
{
Response.Write("<script language='javascript'>alert('Please enter valid mobile number starting with either 7 or 8 or 9!');</script>");
return;
}
}

if (ph.Length >= 8 && ph.Length <= 15)
{
}

else
{
Response.Write("<script language='javascript'>alert('Please enter valid mobile number (min 8, max 15 digits)!');</script>");
return;
}

bool res = Regex.IsMatch(nm, @"^[a-zA-Z ]+$");
if (!res)
{
Response.Write("<script language='javascript'>alert('Please enter valid name!');</script>");
return;
}

bool resNum = Regex.IsMatch(ph, @"^[0-9]+$");
if (!resNum)
{
Response.Write("<script language='javascript'>alert('Please enter valid number!');</script>");
return;
}

if (ddlCountry.Value == "91" && ph.Length != 10)
{
Response.Write("<script language='javascript'>alert('Please enter valid mobile number!');</script>");
return;
}


DataSet dsCheck = new DataSet();
dsCheck = CheckExistingUser(ph);
if (isValidDataset(dsCheck))
{
IPAdress = GetUsersIP();

InsertDetails(nm, ddlCountry.Value, ph, IPAdress, System.DateTime.Now, "1", UserAgent);
Response.Redirect("RegisteredAgain.aspx", false);
}
else
{
IPAdress = GetUsersIP();
InsertDetails(nm, ddlCountry.Value, ph, IPAdress, System.DateTime.Now, "0", UserAgent);
Response.Redirect("NewRegister.aspx", false);
}
}
catch
{
Response.Redirect("OOPS1.aspx", false);
}
}

public DataSet getCountryDetails()
{
try
{
string Sql = string.Empty;
DataSet dsDetails = null;
Sql = "select *,CountryName +' - ' + CountryCode as code from Mobile_Country order by Id";
dsDetails = getDataset(Sql);
return dsDetails;
}
catch (Exception err)
{
throw err;
}
}

public DataSet getDataset(string SQLQuery)
{
try
{
sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
if (sqlConnection.State == ConnectionState.Closed)
{
sqlConnection.Open();
}
if (sqlConnection.State == ConnectionState.Broken)
{
sqlConnection.Close();
sqlConnection.Open();
}
dbAdapter = new SqlDataAdapter(SQLQuery, sqlConnection);
DataSet dsData = new DataSet();
dbAdapter.Fill(dsData);
sqlConnection.Close();
return dsData;
}
catch (Exception err)
{
throw err;
}
}

//protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
//{
// DataSet dsCode = new DataSet();
// if (ddlCountry.DataTextField != "Select")
// {
// dsCode = getCountryCode(ddlCountry.DataTextField);
// if (isValidDataset(dsCode))
// {
// //trLabel.Visible = true;
// lblCode.Value = dsCode.Tables[0].Rows[0]["CountryCode"].ToString();
// }
// else
// {
// //trLabel.Visible = false;
// }
// }
// else
// {
// Response.Redirect("Default.aspx");
// }
//}

public DataSet getCountryCode(string Country)
{
try
{
string Sql = string.Empty;
DataSet dsDetails = null;
Sql = "select top 1 CountryCode from Mobile_Country where CountryName='" + Country + "'";
dsDetails = getDataset(Sql);
return dsDetails;
}
catch (Exception err)
{
throw err;
}
}

public int InsertDetails(string Name, string Country, string Phone, string IP, DateTime RegDate, string IsDupl, string UserAgent)
{
try
{
int res;
string Sql = string.Empty;
Sql = "insert into Mobile_users(Name,Code,Phone,IPAddress,RegistrationDate,IsDuplicate,UserAgent) values('" + Name + "','" + Country + "','" + Phone + "','" + IP + "','" + RegDate + "','" + IsDupl + "','" + UserAgent + "')";
res = insertdataScalar(Sql);
return res;
}
catch (Exception err)
{
throw err;
}
}

public int insertdataScalar(string SQLQuery)
{
object objValue;
int intValue;
try
{
if (sqlConnection.State == ConnectionState.Closed)
{
sqlConnection.Open();
}
if (sqlConnection.State == ConnectionState.Broken)
{
sqlConnection.Close();
sqlConnection.Open();
}
cmd = new SqlCommand(SQLQuery, sqlConnection);
objValue = cmd.ExecuteScalar();
intValue = Convert.ToInt32(objValue);
sqlConnection.Close();
return intValue;
}
catch (Exception err)
{
throw err;
}
}

public DataSet CheckExistingUser(string Phone)
{
try
{
string Sql = string.Empty;
DataSet dsDetails = null;
Sql = "select top 1 Phone from Mobile_users where Phone='" + Phone + "'";
dsDetails = getDataset(Sql);
return dsDetails;
}
catch (Exception err)
{
throw err;
}
}

public bool isValidDataset(DataSet ds)
{
try
{
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
}

public string GetUsersIP()
{
System.Web.HttpContext current = System.Web.HttpContext.Current; string ipAddress = null;

if (current.Request.ServerVariables["HTTP_CLIENT_IP"] != null)

ipAddress = current.Request.ServerVariables["HTTP_CLIENT_IP"];

if (ipAddress == null || ipAddress.Length == 0 || ipAddress == "unknown")

if (current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)

ipAddress = current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

if (ipAddress == null || ipAddress.Length == 0 || ipAddress == "unknown")

if (current.Request.ServerVariables["REMOTE_ADDR"] != null)

ipAddress = current.Request.ServerVariables["REMOTE_ADDR"];

if (ipAddress == null || ipAddress.Length == 0)

ipAddress = "unknown";

return ipAddress;
}

protected void btnReset_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900