I am using visual studio 4.6.01038, and I am pretty new in ASP.NET. I have a very simple registration page where user inputs his name, street and telephone number. I want to control the input in the input box so that user must have to write it in certain way.
1) name/username field must NOT be blank, and must NOT contain numbers
2) street name shall start with letters, then it should be a space, then it should be a number, e.g. street 12
3) the phone number must ONLY contain numbers
If the user choose other than these formats, he will be asked to enter again. Now, all those being said, I am looking for a simpler way to do it. [May be from properties tab of that particular text field?] Or If I do it programmatically, where should I do it and how?
Here is the code I am working on:
Register.aspx:
<form id="form1" runat="server">
<div>
Name<asp:TextBox ID="namebox" runat="server" OnTextChanged="namebox_TextChanged"></asp:TextBox>
<br /> <br />
Street<asp:TextBox ID="streetbox" runat="server"></asp:TextBox>
<br /> <br />
Phone Number <asp:TextBox ID="phonebox" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Register" OnClick="Button1_Click" /><br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br /><br />
</div>
</form>
Register.aspx.cs:
protected void Button1_Click(object sender, EventArgs e)//register button
{
try
{
SqlCommand myCommand = new SqlCommand();
//SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings("Data Source=(LocalDB);MSSQLLocalDB;AttachDbFilename=|DataDirectory|;Database.mdf;Integrated Security=True"));
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\lab1.mdf;Integrated Security=True");
//SqlConnection conn = new SqlConnection("ConnectionStringBooks");
conn.Open();
myCommand = new SqlCommand("INSERT INTO userdata(username, street, telephonenum) VALUES ('" + namebox.Text + "','" + streetbox.Text + "','" + phonebox.Text + "')", conn);
myCommand.ExecuteNonQuery();
Response.Redirect("~/Store.aspx?name="+namebox.Text+"");
}
catch (Exception ex)
{
Label1.Text = ex.Message;
Label1.Visible=true;
}
}
street name shall start with letters, then it should be a space, then it should be a numberI strongly urge you to read Falsehoods programmers believe about addressesthe phone number must ONLY contain numbersditto Falsehoods Programmers Believe About Phone Numbers