如何将表达式与asp.net中的文本框匹配 [英] how to match expression with textbox in asp.net

查看:81
本文介绍了如何将表达式与asp.net中的文本框匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的reg表达式

reg = ^ [0-9] {10} $我需要与文本框匹配的此reg值

解决方案

我需要与文本框匹配的此reg值

有很多方法可以在文本框中匹配表达式:

1)编写服务器端代码以进行匹配
2)为文本框声明一个RegularExpressionValidator
3)使用Javascript

1)服务器端代码:

 使用系统;
使用 System.Text.RegularExpressions;

    无效 CheckTextBoxValue()
    {
// 首先,我们看到输入字符串.
字符串 input = TextBox1.text;
// 在这里,我们将其称为Regex.Match.
匹配match = Regex.Match(input, @" ,
RegexOptions.IgnoreCase);
// 在这里,我们检查Match实例.
如果(匹配成功)
{
// 最后在此处写下您的相关代码.
}
    } 



2)RegularExpressionValidator:这是一个示例.请相应地进行更改.

 <   asp:textbox     id   ="     runat   =" 服务器" 验证组  检查"  xmlns: asp   ="  >  <  /asp:textbox  > 
<   asp:regularexpressionvalidator     id   ="   runat   服务器"  controltovalidate    TextBox1"  xmlns:asp   ="  > 
ErrorMessage =请仅输入数字" ValidationExpression ="^ \ d + 


This is my reg expression

reg=^[0-9]{10}$ this reg values i need to match with textbox

解决方案

this reg values i need to match with textbox


There are many ways to match the expression in textbox:

1) Write Server-side code to match
2) Declare a RegularExpressionValidator for the textbox
3) Use Javascript

1) Server side code:

using System;
using System.Text.RegularExpressions;

    void CheckTextBoxValue()
    {
	// First we see the input string.
	string input = TextBox1.text;
	// Here we call Regex.Match.
	Match match = Regex.Match(input, @"^[0-9]{10}


", RegexOptions.IgnoreCase); // Here we check the Match instance. if (match.Success) { // Finally Write your relevant code here. } }



2) RegularExpressionValidator: This is an example. Please make your changes accordingly.

<asp:textbox id="TextBox1" runat="server" validationgroup="check" xmlns:asp="#unknown"></asp:textbox>
<asp:regularexpressionvalidator id="RegularExpressionValidator1" runat="server" controltovalidate="TextBox1" xmlns:asp="#unknown">
ErrorMessage="Please Enter Only Numbers" ValidationExpression="^\d+


这篇关于如何将表达式与asp.net中的文本框匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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