asp.net的正则表达式验证器失败 [英] regular expression validator of asp.net fails

查看:98
本文介绍了asp.net的正则表达式验证器失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了2页登录并注册了..我在标志页面上使用了正则表达式验证器,用于名字,中间名和姓。如果我输入数字,它会给我错误。但是当我点击注册按钮时,即使输入数字后我的记录也会被插入。请帮助

I have created 2pages login and sign up.. I have used regular expression validator on sign page for first name,middle name and surname..It is giving me error if i enter digits. but when i click on sign up button my record gets inserted even after entering digits. please help

推荐答案

根据您的正则表达式验证,如果您想在ValidationExpression中添加0-9中的SurName文本框中的数字,则文本框不接受数字\d。



在按钮点击功能背后的代码中添加一个条件

if(Page.IsValid)然后只生成数据库调用将文本值插入数据库。
As per your regular expression validation the textbox doesn't accepts digits, if you want to have digits in SurName textbox in the ValidationExpression add 0-9 or \d.

In the code behind on the button click function add a condition
if(Page.IsValid) then only make DB call to insert the text values into database.


您的代码应如下所示



SignUp.aspx

Your code should look like this

SignUp.aspx
<asp:TextBox ID="txtSur" runat="server"></asp:TextBox>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="Invalid Surname" Enabled="True" ControlToValidate="txtSur" ForeColor="Red" ValidationExpression="\b[A-Za-z0-9]+\b" Display="Dynamic" SetFocusOnError="True">
    <asp:Button ID="SignUpButton"

        runat="server" Text="Sign Up" onclick="SignUpButton_Click" />







在SignUp.aspx中.cs




In SignUp.aspx.cs

protected void SignUpButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                //Make a DB call and insert the text values into the DB
            }
        }


这篇关于asp.net的正则表达式验证器失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆