在下面的代码中我有一个隐藏字段和一个文本框。我想将正则表达式存储在隐藏字段中,并与我在textbox中输入的值进行比较。但是当我执行它时,我可以打印常规隐藏字段中的表达式。当我在textnbox中键入任何值时,警告信息在隐藏字段中显示未定义或空白警报。请帮助我解决问题。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (FieldTypeInfo == FieldType.TextBox)
{
lblText.Text = FieldLabel;
txtreq.Enabled = this.IsMandatory;
string strRegularExp = string.Empty;
if (ListOfRegularExpression != null)
{
for (int iRow = 0; iRow < ListOfRegularExpression.Count; iRow++)
{
strRegularExp += ListOfRegularExpression[iRow].ToString() + "~~";
//hidRegExp.Value = strRegularExp;
if (iRow == ListOfRegularExpression.Count - 1)
{
strRegularExp = strRegularExp.TrimEnd("~~".ToCharArray());
txtField.Attributes.Add("onblur", "javascript:ValidateRegExp('" + txtField.ToString() + "');");
}
}
}
hidRegExp.Value = strRegularExp;
lbl.Text = hidRegExp.Value;
}}
js:
function ValidateRegExp(txtInput) {
var hiddenValue = document.getElementById("<%=hidRegExp.ClientID%>").value;
alert(hiddenValue);
var mySplitResult = new Array();
mySplitResult = hiddenValue.split("~~");
for (i = 0; i < mySplitResult.length; i++) {
//document.write("<br /> Array[" + i + " ]= " + mySplitResult[i]);
var re = new RegExp(mySplitResult[i]);
if (txtInput.match(re)) {
// alert("Successful match");
} else {
// alert("No match");
}
}}
<asp:HiddenField ID="hidRegExp" runat="server" ClientIDMode="static" EnableViewState="true" Value="">
</asp:HiddenField >
<asp:TextBox ID="txtField" runat="server" CausesValidation="true"></asp:TextBox>