如果只允许文本框中的数字,如果尝试输入字母表应该在webform中显示消息。 C#ASP.NET [英] How to allow only numbers in textbox, if trying to enter alphabets should display messge in webform. C# ASP.NET
问题描述
我想只允许数字以外的文本框中的数字接受,我的意思是不应该允许输入,需要显示messege。
<%@ Page Title =Language =C#MasterPageFile =〜/ Expense.MasterAutoEventWireup =trueCodeBehind =Register.aspx.csInherits =Expense_System.Register%>
< asp:Content ID =Content1ContentPlaceHolderID =headrunat =server>
< style type =text / css>
.auto-style1 {
宽度:39%;
}
.auto-style2 {
text-align:right;
宽度:138px;
}
.auto-style3 {
text-align:left;
宽度:143px;
}
.auto-style4 {
text-align:right;
宽度:138px;
身高:30px;
}
.auto-style5 {
text-align:left;
身高:30px;
宽度:143px;
}
.auto-style7 {
text-align:right;
宽度:138px;
身高:29px;
}
.auto-style8 {
text-align:left;
身高:29px;
宽度:143px;
}
.auto-style10 {
text-align:right;
宽度:138px;
身高:27px;
}
.auto-style11 {
text-align:left;
身高:27px;
宽度:143px;
}
.auto-style13 {
text-align:right;
宽度:138px;
身高:28px;
}
.auto-style14 {
text-align:left;
身高:28px;
宽度:143px;
}
.auto-style16 {
text-align:left;
身高:27px;
宽度:165px;
margin-left:80px;
}
.auto-style17 {
text-align:left;
宽度:165px;
}
.auto-style18 {
text-align:left;
身高:29px;
宽度:165px;
}
.auto-style19 {
text-align:left;
身高:28px;
宽度:165px;
}
.auto-style20 {
text-align:left;
身高:30px;
宽度:165px;
}
< / style>
< / asp:Content>
< asp:Content ID =Content2ContentPlaceHolderID =ContentPlaceHolder1runat =server>
< asp:菜单ID =Menu1runat =serverBackColor =#FFFBD6DynamicHorizontalOffset =2字体名称=Verdana字体大小= 0.8emForeColor =#990000Orientation =HorizontalRenderingMode =TableStaticSubMenuIndent =10pxstyle =font-weight:700; color:#FFFFFF; background-color:#99CCFF; font-size:xx -large; text-align:center;宽度=99%高度=67px>
< DynamicHoverStyle BackColor =#990000ForeColor =White/>
< DynamicMenuItemStyle HorizontalPadding =5pxVerticalPadding =2px/>
< DynamicMenuStyle BackColor =#FFFBD6/>
< DynamicSelectedStyle BackColor =#FFCC66/>
< Items>
<% - < asp:MenuItem NavigateUrl =〜/ HomePage.aspxText =Home PageValue =Home Page>< / asp:MenuItem> - %>
< asp:MenuItem NavigateUrl =〜/ Loginpage.aspxText =LoginValue =Login>< / asp:MenuItem>
< / Items>
< StaticHoverStyle BackColor =#990000ForeColor =White/>
< StaticMenuItemStyle HorizontalPadding =5pxVerticalPadding =2px/>
< StaticSelectedStyle BackColor =#FFCC66/>
< / asp:菜单>
< h1>填写以下详细信息以注册< / h1>
< table class =auto-style1border =1>
< tr>
< td class =auto-style10>全名:< / td>
< td class =auto-style16>
< asp:TextBox ID =tbfullnamerunat =serverWidth =160px>< / asp:TextBox>
< / td>
< td class =auto-style11>
< asp:RequiredFieldValidator ID =RequiredFieldValidatorNamerunat =servercontroltovalidate =tbfullnameErrorMessage =名称是必需的ForeColor =Red>< / asp:RequiredFieldValidator>
< / td>
< / tr>
< tr>
< td class =auto-style2>电子邮件ID:< / td>
< td class =auto-style17>
< asp:TextBox ID =tbemailrunat =serverTextMode =EmailWidth =160px>< / asp:TextBox>
< / td>
< td class =auto-style3>
< asp:RequiredFieldValidator ID =RequiredFieldValidatormailrunat =serverControlToValidate =tbemailErrorMessage =需要电子邮件ForeColor =Red>< / asp:RequiredFieldValidator>
< asp:RegularExpressionValidator ID =RegularExpressionValidatormailrunat =serverControlToValidate =tbemailErrorMessage =无效的电子邮件ValidationExpression =\ w +([ - +。'] \ w +)* @ \\ \\ w +([ - 。] \ w +)* \.\\\ +([ - 。] \ w +)*ForeColor =Red>< / asp:RegularExpressionValidator>
< / td>
< / tr>
< tr>
< td class =auto-style7>电话号码:< / td>
< td class =auto-style18>
< asp:TextBox ID =tbphonenorunat =serverTextMode =PhoneWidth =160pxOnTextChanged =tbphoneno_TextChanged>< / asp:TextBox>
< / td>
< td class =auto-style8>
< asp:RegularExpressionValidator ID =RegularExpressionValidatorphonenorunat =serverControlToValidate =tbphonenoErrorMessage =电话号码无效ValidationExpression =[0-9] {10}ForeColor =Red> ;< / ASP:的RegularExpressionValidator>
< asp:RequiredFieldValidator ID =RequiredFieldValidatorphonenorunat =serverControlToValidate =tbphonenoErrorMessage =电话号码是必需的ForeColor =Red>< / asp:RequiredFieldValidator>
< / td>
< / tr>
< tr>
< td class =auto-style13>出生日期:< / td>
< td class =auto-style19>
< asp:TextBox ID =tbdobrunat =serverTextMode =DateWidth =160px>< / asp:TextBox>
< / td>
< td class =auto-style14>
< asp:RequiredFieldValidator ID =RequiredFieldValidatordobrunat =serverControlToValidate =tbdobErrorMessage =出生日期是必需的ForeColor =Red>< / asp:RequiredFieldValidator>
< / td>
< / tr>
< tr>
< td class =auto-style2>登录ID:< / td>
< td class =auto-style17>
< asp:TextBox ID =tbloginidrunat =serverWidth =160px>< / asp:TextBox>
< / td>
< td class =auto-style3>
< asp:RequiredFieldValidator ID =RequiredFieldValidatorloginidrunat =serverControlToValidate =tbloginidErrorMessage =登录ID是必需的ForeColor =Red>< / asp:RequiredFieldValidator>
< / td>
< / tr>
< tr>
< td class =auto-style4>密码:< / td>
< td class =auto-style20>
< asp:TextBox ID =tbpwdrunat =serverTextMode =PasswordWidth =160px>< / asp:TextBox>
< / td>
< td class =auto-style5>
< asp:RequiredFieldValidator ID =RequiredFieldValidatorpwdrunat =serverControlToValidate =tbpwdErrorMessage =password is RequiredForeColor =Red>< / asp:RequiredFieldValidator>
< / td>
< / tr>
< tr>
< td class =auto-style4>确认密码:< / td>
< td class =auto-style20>
< asp:TextBox ID =tbconfirmpwdrunat =serverTextMode =PasswordWidth =160px>< / asp:TextBox>
< / td>
< td class =auto-style5>
< asp:RequiredFieldValidator ID =RequiredFieldValidatorconpwdrunat =serverControlToValidate =tbpwdErrorMessage =密码是必需的ForeColor =Red>< / asp:RequiredFieldValidator>
< / td>
< / tr>
< tr>
< td class =auto-style4> < / td>
< td class =auto-style20>
< asp:Button ID =btnRegsubmitrunat =serverText =SignUpOnClick =btnRegsubmit_Click/>
< input id =RegResettype =resetvalue =Reset/>< / td>
< td class =auto-style5>
< / td>
< / tr>
< / table>
< / asp:Content>
<$ p $使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Data.SqlClient;
使用System.Configuration;
使用System.Data;
使用System.Text.RegularExpressions;
命名空间Expense_System
{
公共部分类注册:System.Web.UI.Page
{
protected void Page_Load(object sender ,EventArgs e)
{
}
protected void btnRegsubmit_Click(object sender,EventArgs e)
{
String strConnString = ConfigurationManager.ConnectionStrings [conString]。ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText =[dbo]。[SpCreateUser];
cmd.Parameters.Add(@ Full_name,SqlDbType.VarChar).Value = tbfullname.Text;
cmd.Parameters.Add(@ Dob,SqlDbType.Date).Value = tbdob.Text;
cmd.Parameters.Add(@ email,SqlDbType.VarChar).Value = tbemail.Text;
cmd.Parameters.Add(@ phoneno,SqlDbType.Int).Value = tbphoneno.Text;
cmd.Parameters.Add(@ loginid,SqlDbType.VarChar).Value = tbloginid.Text;
cmd.Parameters.Add(@ pwd,SqlDbType.VarChar).Value = tbpwd.Text;
cmd.Connection = con;
int totalrecordscreated =(int)cmd.ExecuteNonQuery();
Response.Write(No of created records+ totalrecordscreated.ToString());
con.Close();
}
protected void tbphoneno_TextChanged(object sender,EventArgs e)
{
if( !Regex.IsMatch(tbphoneno.Text,
@(^([0-9] * | \d * \d {1}?\d *)$)))
{
}
其他
{
}
}
}
}
我的尝试:
protected void tbphoneno_TextChanged(object sender,EventArgs e)
{
if(!Regex.IsMatch(tbphoneno.Text,$ b $) b @(^([0-9] * | \d * \d {1}?\d *)$)))
{
}
$
{
}
这是正确的吗?但是没有验证。
)))
{
}
else
{
}
}
}
}
我尝试过:
protected void tbphoneno_TextChanged(对象发送者,EventArgs e)
{
if(!Regex.IsMatch(tbphoneno.Text,
@(^([0-9] * | \d * \d { 1}?\d *)
)))
{
}
else
{
}
这是正确的吗?但没有验证。
< blockquote>基本上,不要在C#中执行 - 只在服务器上运行,这意味着每次用户按下一个键时,都需要回发到服务器以进行验证。这是缓慢而低效的 - 执行此操作在va的客户端lidation全部是本地的:仅限javascript数字输入字段 - Google搜索 [ ^ ]
i want to allow only numbers in textbox other than number should accept, i mean should not allow to type, messege need to be displayed.
<%@ Page Title="" Language="C#" MasterPageFile="~/Expense.Master" AutoEventWireup="true" CodeBehind="Register.aspx.cs" Inherits="Expense_System.Register" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> <style type="text/css"> .auto-style1 { width: 39%; } .auto-style2 { text-align: right; width: 138px; } .auto-style3 { text-align: left; width: 143px; } .auto-style4 { text-align: right; width: 138px; height: 30px; } .auto-style5 { text-align: left; height: 30px; width: 143px; } .auto-style7 { text-align: right; width: 138px; height: 29px; } .auto-style8 { text-align: left; height: 29px; width: 143px; } .auto-style10 { text-align: right; width: 138px; height: 27px; } .auto-style11 { text-align: left; height: 27px; width: 143px; } .auto-style13 { text-align: right; width: 138px; height: 28px; } .auto-style14 { text-align: left; height: 28px; width: 143px; } .auto-style16 { text-align: left; height: 27px; width: 165px; margin-left: 80px; } .auto-style17 { text-align: left; width: 165px; } .auto-style18 { text-align: left; height: 29px; width: 165px; } .auto-style19 { text-align: left; height: 28px; width: 165px; } .auto-style20 { text-align: left; height: 30px; width: 165px; } </style> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> <asp:Menu ID="Menu1" runat="server" BackColor="#FFFBD6" DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#990000" Orientation="Horizontal" RenderingMode="Table" StaticSubMenuIndent="10px" style="font-weight: 700; color: #FFFFFF; background-color: #99CCFF; font-size: xx-large; text-align: center;" Width="99%" Height="67px"> <DynamicHoverStyle BackColor="#990000" ForeColor="White" /> <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" /> <DynamicMenuStyle BackColor="#FFFBD6" /> <DynamicSelectedStyle BackColor="#FFCC66" /> <Items> <%-- <asp:MenuItem NavigateUrl="~/HomePage.aspx" Text="Home Page" Value="Home Page"></asp:MenuItem>--%> <asp:MenuItem NavigateUrl="~/Loginpage.aspx" Text="Login" Value="Login"></asp:MenuItem> </Items> <StaticHoverStyle BackColor="#990000" ForeColor="White" /> <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" /> <StaticSelectedStyle BackColor="#FFCC66" /> </asp:Menu> <h1> Fill the below details to register</h1> <table class="auto-style1" border="1"> <tr> <td class="auto-style10">Full Name:</td> <td class="auto-style16"> <asp:TextBox ID="tbfullname" runat="server" Width="160px" ></asp:TextBox> </td> <td class="auto-style11"> <asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" controltovalidate="tbfullname" ErrorMessage="Name is mandatory" ForeColor="Red"></asp:RequiredFieldValidator> </td> </tr> <tr> <td class="auto-style2">Email id:</td> <td class="auto-style17"> <asp:TextBox ID="tbemail" runat="server" TextMode="Email" Width="160px"></asp:TextBox> </td> <td class="auto-style3"> <asp:RequiredFieldValidator ID="RequiredFieldValidatormail" runat="server" ControlToValidate="tbemail" ErrorMessage="Email Required" ForeColor="Red"></asp:RequiredFieldValidator> <asp:RegularExpressionValidator ID="RegularExpressionValidatormail" runat="server" ControlToValidate="tbemail" ErrorMessage="invalid email" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ForeColor="Red"></asp:RegularExpressionValidator> </td> </tr> <tr> <td class="auto-style7">Phone Number:</td> <td class="auto-style18"> <asp:TextBox ID="tbphoneno" runat="server" TextMode="Phone" Width="160px" OnTextChanged="tbphoneno_TextChanged" ></asp:TextBox> </td> <td class="auto-style8"> <asp:RegularExpressionValidator ID="RegularExpressionValidatorphoneno" runat="server" ControlToValidate="tbphoneno" ErrorMessage="Phone number is invalid" ValidationExpression="[0-9]{10}" ForeColor="Red"></asp:RegularExpressionValidator> <asp:RequiredFieldValidator ID="RequiredFieldValidatorphoneno" runat="server" ControlToValidate="tbphoneno" ErrorMessage="Phone number is Required" ForeColor="Red"></asp:RequiredFieldValidator> </td> </tr> <tr> <td class="auto-style13">Date of birth:</td> <td class="auto-style19"> <asp:TextBox ID="tbdob" runat="server" TextMode="Date" Width="160px"></asp:TextBox> </td> <td class="auto-style14"> <asp:RequiredFieldValidator ID="RequiredFieldValidatordob" runat="server" ControlToValidate="tbdob" ErrorMessage="Date of birth is Required" ForeColor="Red"></asp:RequiredFieldValidator> </td> </tr> <tr> <td class="auto-style2">Login id:</td> <td class="auto-style17"> <asp:TextBox ID="tbloginid" runat="server" Width="160px"></asp:TextBox> </td> <td class="auto-style3"> <asp:RequiredFieldValidator ID="RequiredFieldValidatorloginid" runat="server" ControlToValidate="tbloginid" ErrorMessage="Login id is Required" ForeColor="Red"></asp:RequiredFieldValidator> </td> </tr> <tr> <td class="auto-style4">Password:</td> <td class="auto-style20"> <asp:TextBox ID="tbpwd" runat="server" TextMode="Password" Width="160px"></asp:TextBox> </td> <td class="auto-style5"> <asp:RequiredFieldValidator ID="RequiredFieldValidatorpwd" runat="server" ControlToValidate="tbpwd" ErrorMessage="password is Required" ForeColor="Red"></asp:RequiredFieldValidator> </td> </tr> <tr> <td class="auto-style4">Confirm Password:</td> <td class="auto-style20"> <asp:TextBox ID="tbconfirmpwd" runat="server" TextMode="Password" Width="160px"></asp:TextBox> </td> <td class="auto-style5"> <asp:RequiredFieldValidator ID="RequiredFieldValidatorconpwd" runat="server" ControlToValidate="tbpwd" ErrorMessage="Password is Required" ForeColor="Red"></asp:RequiredFieldValidator> </td> </tr> <tr> <td class="auto-style4"> </td> <td class="auto-style20"> <asp:Button ID="btnRegsubmit" runat="server" Text="SignUp" OnClick="btnRegsubmit_Click" /> <input id="RegReset" type="reset" value="Reset" /></td> <td class="auto-style5"> </td> </tr> </table> </asp:Content>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Configuration; using System.Data; using System.Text.RegularExpressions; namespace Expense_System { public partial class Register : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnRegsubmit_Click(object sender, EventArgs e) { String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString; SqlConnection con = new SqlConnection(strConnString); con.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "[dbo].[SpCreateUser]"; cmd.Parameters.Add("@Full_name", SqlDbType.VarChar).Value = tbfullname.Text; cmd.Parameters.Add("@Dob", SqlDbType.Date).Value = tbdob.Text; cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = tbemail.Text; cmd.Parameters.Add("@phoneno", SqlDbType.Int).Value = tbphoneno.Text; cmd.Parameters.Add("@loginid", SqlDbType.VarChar).Value = tbloginid.Text; cmd.Parameters.Add("@pwd", SqlDbType.VarChar).Value = tbpwd.Text; cmd.Connection = con; int totalrecordscreated =(int)cmd.ExecuteNonQuery(); Response.Write("No of created records " + totalrecordscreated.ToString()); con.Close(); } protected void tbphoneno_TextChanged(object sender, EventArgs e) { if (!Regex.IsMatch(tbphoneno.Text, @"(^([0-9]*|\d*\d{1}?\d*)$)")) { } else { } } } }
What I have tried:
protected void tbphoneno_TextChanged(object sender, EventArgs e) { if (!Regex.IsMatch(tbphoneno.Text, @"(^([0-9]*|\d*\d{1}?\d*)$)")) { } else { }
Is this correct? but not validating.
)")) { } else { } } } }
What I have tried:
protected void tbphoneno_TextChanged(object sender, EventArgs e) { if (!Regex.IsMatch(tbphoneno.Text, @"(^([0-9]*|\d*\d{1}?\d*)
)")) { } else { }
Is this correct? but not validating.
Basically, don't do it in C# - that only runs at the Server, and that means each time the user presses a key, you need a post back to the server to to the validation. That's slow and inefficient - do it in the Client where the validation is all local: javascript numeric only input field - Google Search[^]
这篇关于如果只允许文本框中的数字,如果尝试输入字母表应该在webform中显示消息。 C#ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!