- C# code
using System;using System.Collections;using System.Configuration;using System.Data;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using WF_BLL;using WF_Model;using System.Text.RegularExpressions;public partial class Login : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { }protected void btn_Login_Click(object sender, EventArgs e) { if (!(checking(this.txt_Username.Text.Trim()) || checkings(this.txt_Username.Text.Trim()))) { Response.Write("<script> alert('只能是数字、字母、下划线、汉字!')</script>"); } else { UserInfo info = new UserInfo(); info.UserName = this.txt_Username.Text.Trim(); info.PassWord = this.txt_Password.Text.Trim(); Login_BLL login = new Login_BLL(); bool value = login.Login(info); if (!value) { Response.Write("<script> alert('注册成功!')</script>"); } else { Response.Write("<script> alert('注册失败!')</script>"); } } } public static bool checking(string strings) { return Regex.IsMatch(strings, @"^[0-9a-zA-Z_]+$"); } public static bool checkings(string strings) { return Regex.IsMatch(strings, @"^[\u4e00-\u9fa5],{0,}$"); }}
我想要验证输入的字符串是不是数字、字母、下划线或者是汉字。要是不是上诉四种,就弹出提示,不让注册!
该怎么写啊!!!!
请各位,赐教!!!!!!!!!!!!!!!!!!!!!!!!!
感激不尽!!!!!
------解决方案--------------------------------------------------------
楼主怎么不在js里面判断呢!你这种在服务器端验证的不是影响效率吗?
------解决方案--------------------------------------------------------
Regex.IsMatch(strings, @"^\w+$");
------解决方案--------------------------------------------------------
^([\u4e00-\u9fa50-9a-zA-Z_])$
------解决方案--------------------------------------------------------
正则表达式
------解决方案--------------------------------------------------------
验证空间 RegularExpressionValidator 将正则表达式写在属性ValidatonExpression中
------解决方案--------------------------------------------------------
------解决方案--------------------------------------------------------
弹出窗体:
Page.ClientScript.RegisterStartupScript(this.GetType(), "UpdateSucceed", "<script>alert('Update time succeed!')</script>");
------解决方案--------------------------------------------------------
Regex str2 = new Regex(@"^[\w\u4E00-\u9FFF]+$");
------解决方案--------------------------------------------------------
现在验证的基本3方法:
1.验证控件
2.正则表达式
3.自己根据需要写函数或者类。
------解决方案--------------------------------------------------------
------解决方案--------------------------------------------------------
实在还是可以用asp.net的验证控件加上正则表达式