提问人:OMKAR AGRAWAL 提问时间:7/9/2017 最后编辑:OMKAR AGRAWAL 更新时间:2/17/2021 访问量:372
Regexvalidator 的 validaterequestmode=enabled 在 asp.net 中无法正常工作
Regexvalidator's validaterequestmode=enabled not working properly in asp.net
问:
注意:如果需要,可以查看我的项目(我遇到的问题在Login-Signup.aspx页面中。点击 这里 进入项目。
我在我的网络表单中添加了一些正则表达式验证器控件。我已将其属性validaterequestmode设置为enabled并检查了它。它曾经完美地工作,我的意思是,如果我的名字文本框中有一个空格,那么它在那个实例中会显示错误(不改变焦点),其他人也是如此。我在我的 js 中添加了一些函数,我什至不可能链接它们,现在当我运行页面时,除非我更改焦点,否则它不会验证输入。请帮我解决这个问题。
注意:我对正则表达式没有任何问题,这只是我所关心的validaterequestmode属性。
我在下面添加我的html代码: (用于注册视图)
//Donot uncomment any line. dare not uncommenting any line untill permission granted.
// Donot use the access key and the function verify() untill permission granted.
// set endpoint and your access key
var access_key = 'my_key';
var email_address;
var valid_format, smtp_check, disposable, mxfound, Urole;
function setmail() {
email_address = document.getElementById('TextBox11').value;
// document.getElementById('Label5').textContent = email_address;
verify();
}
// verify email address via AJAX call
function verify() {
$.ajax({
url: 'http://apilayer.net/api/check?access_key=' + access_key + '&email=' + email_address,
dataType: 'jsonp',
success: function validate(json) {
//Access and use your preferred validation result objects
console.log(json.format_valid);
console.log(json.smtp_check);
console.log(json.score);
console.log("disposable: " + json.disposable);
console.log(json.role);
console.log(json.mxfound);
//valid_format = json.valid_format ;
//smtp_check = json.smtp_check ;
//disposable = json.disposable;
//mxfound = json.mxfound ;
//Urole = json.role;
document.getElementById("HFformatvalid").innerHTML = json.valid_format;
document.getElementById("HFsmtpcheck").innerHTML = json.smtp_check;
document.getElementById("HFdisposable").innerHTML = json.disposable;
document.getElementById("HFmxfound").innerHTML = json.mxfound;
document.getElementById("HFrole").innerHTML = json.role;
emailchck("HFformatvalid", "HFsmtpcheck", "HFdisposable", "HFmxfound", "HFrole");
}
});
}
function emailchck(id1, id2, id3, id4, id5) {
valid_format = document.getElementById(id1).innerText;
smtp_check = document.getElementById(id2).innerText;
disposable = document.getElementById(id3).innerText;
mxfound = document.getElementById(id4).innerText;
Urole = document.getElementById(id5).innerText;
if ((valid_format == "true" && smtp_check == "true" && disposable == "false" && mxfound == "true" && Urole == "false")) {
//document.getElementById("HFemail").innerText = document.getElementById("TextBox11").innerText;
document.getElementById("HFemail").innerText = email_address;
document.getElementById("Label4").innerHtml = email_address + "<br />disposable= " + disposable + "<br />Smtp check= " + smtp_check + "<br /> mxfound= " + mxfound + "<br />Urole= " + Urole + "<br />valid format= " + valid_format;
} else {
// document.getElementById("Label17").innerText = "";
if (valid_format == "false") {
document.getElementById("Label17").innerHTML += "Invalid email format" + "<br />";
}
if (smtp_check == "false") {
document.getElementById("Label17").innerHTML += "Invalid email" + "<br />";
}
if (disposable == "true") {
document.getElementById("Label17").innerHTML += "Please Donot use diaposable email" + "<br />";
}
if (mxfound == "false") {
document.getElementById("Label17").innerHTML += "Cannot send email to your mail. Please use a genuine email id" + "<br />";
}
if (Urole == "true") {
document.getElementById("Label17").innerHTML += "Invalid email.Use proper email id" + "<br />";
}
}
}
function assign(idfrom, idto) {
document.getElementById(idto).innerText = document.getElementById(idfrom).innerText;
}
function verify() {
}
//document.getElementById('SUSbmtBtn').onclick(function () {
// email_address = document.getElementById('TextBox11').value;
// document.getElementById('Label5').textContent = email_address;
//});
<%@ Page Language="C#" UnobtrusiveValidationMode="none" AutoEventWireup="true" CodeBehind="Login-Signup.aspx.cs" Inherits="Abhiyantrix.Login_Signup" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<%--<link href="Content/bootstrap.min.css" rel="stylesheet" />
<link href="Content/bootstrap-theme.min.css" rel="stylesheet" />--%>
<%--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />--%>
<link href="Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body style="background-color: #DFE2DB">
<form id="form1" runat="server">
<asp:HiddenField ID="HFformatvalid" runat="server" />
<asp:HiddenField ID="HFsmtpcheck" runat="server" />
<asp:HiddenField ID="HFdisposable" runat="server" />
<asp:HiddenField ID="HFmxfound" runat="server" />
<asp:HiddenField ID="HFrole" runat="server" />
<asp:HiddenField ID="HFaccept" runat="server" />
<asp:HiddenField ID="HFfname" runat="server" />
<asp:HiddenField ID="HFsname" runat="server" />
<asp:HiddenField ID="HFemail" runat="server" />
<asp:HiddenField ID="HFpass" runat="server" />
<asp:HiddenField ID="HFphno" runat="server" />
<asp:HiddenField ID="HFut" runat="server" />
<div>
<asp:Label ID="Label4" runat="server"></asp:Label>
<br />
<br />
<asp:Label ID="Label5" runat="server"></asp:Label>
<br />
<br />
<asp:MultiView ID="MultiView1" runat="server">
<asp:View ID="Login" runat="server" OnActivate="Page_Load">
<div style="background-color: #FFF056">
<asp:Label ID="Label8" runat="server" Font-Bold="True" Font-Italic="True" Font-Size="Larger" Text="Email"></asp:Label>
<asp:TextBox ID="TextBox7" runat="server" ValidateRequestMode="Enabled" ValidationGroup="LoginIn" TextMode="Email" class="input input-sm form-control" placeholder="Enter your email here"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="TextBox7" ErrorMessage="*Enter Your Email ID" ValidationGroup="LoginIn"></asp:RequiredFieldValidator>
</div>
<br />
<br />
<div style="background-color: #FFF056">
<asp:Label ID="Label9" runat="server" Font-Bold="True" Font-Italic="True" Font-Size="Larger" Text="Password"></asp:Label>
<asp:TextBox ID="TextBox8" runat="server" ValidateRequestMode="Enabled" ValidationGroup="LoginIn" TextMode="Password" class="input input-sm form-control" placeholder="Enter your password here"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="TextBox8" ErrorMessage="*Enter your password" ValidationGroup="LoginIn"></asp:RequiredFieldValidator>
</div>
<br />
<br />
<div style="background-color: #FFF056">
<asp:Button ID="Button1" runat="server" Height="45px" Text="Login" Width="118px" ValidationGroup="LoginIn" OnClick="Button1_Click" class="btn btn-danger btn-lg" />
</div>
<br />
<br />
<br />
<div style="background-color: #D9853B">
<asp:Button ID="Button3" runat="server" Height="44px" OnClick="Button3_Click" Text="Signup" Width="105px" UseSubmitBehavior="False" ValidationGroup="ChangeView" ViewStateMode="Enabled" class="btn btn-warning btn-lg" />
</div>
</asp:View>
<asp:View ID="Signup" runat="server">
<div style="background-color: #A2AB58">
<div style="background-color: #D9853B">
<asp:Label ID="Label10" runat="server" Font-Bold="True" Font-Italic="True" Font-Size="Larger" Text="First Name"></asp:Label>
<asp:TextBox ID="TextBox9" runat="server" Font-Size="Larger" ValidateRequestMode="Enabled" ValidationGroup="SignUp" MaxLength="10" class="input input-sm form-control" Columns="4" placeholder="Enter your first name here" onblur="assign(this.id,HFfname)"
AutoCompleteType="FirstName" CausesValidation="True"></asp:TextBox>
</div>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox9" ErrorMessage="*Please enter your first name" ForeColor="Red" ValidationGroup="SignUp" SetFocusOnError="True" ValidateRequestMode="Enabled" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ControlToValidate="TextBox9" ErrorMessage="Only alphabets allowed; upto 15 alphabets with no spaces." ForeColor="Red" SetFocusOnError="True" ValidateRequestMode="Enabled"
ValidationExpression="^([a-z,A-Z]{1,10}$)" Display="Dynamic" EnableClientScript="False"></asp:RegularExpressionValidator>
<br />
<br />
<div style="background-color: #D9853B">
<asp:Label ID="Label11" runat="server" Font-Bold="True" Font-Italic="True" Font-Size="Larger" Text="Last Name"></asp:Label>
<asp:TextBox ID="TextBox10" runat="server" Font-Size="Larger" ValidateRequestMode="Enabled" ValidationGroup="SignUp" MaxLength="10" class="input input-sm form-control" placeholder="Enter your last name here" onblur="assign(this.id,HFsname)"></asp:TextBox>
</div>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox10" ErrorMessage="*Please enter your last name" ForeColor="Red" ValidationGroup="SignUp" SetFocusOnError="True" ValidateRequestMode="Enabled" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ControlToValidate="TextBox10" ErrorMessage="Only alphabets allowed; upto 15 alphabets with no spaces." ForeColor="Red" SetFocusOnError="True" ValidateRequestMode="Enabled"
ValidationExpression="^([a-z,A-Z]{1,10}$)" ValidationGroup="SignUp" Display="Dynamic"></asp:RegularExpressionValidator>
<br />
<br />
<div style="background-color: #D9853B">
<asp:Label ID="Label12" runat="server" Font-Bold="True" Font-Italic="True" Font-Size="Larger" Text="Email"></asp:Label>
<asp:TextBox ID="TextBox11" runat="server" Font-Size="Larger" ValidationGroup="SignUp" OnTextChanged="TextBox11_TextChanged" class="input input-sm form-control" placeholder="Enter your email name here" name="TextBox11" onblur="setmail()" MaxLength="50"></asp:TextBox>
</div>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="TextBox11" ErrorMessage="*Check your Email -ID" ForeColor="Red" ValidationGroup="SignUp" SetFocusOnError="True" ValidateRequestMode="Enabled" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="TextBox11" ErrorMessage="*Check youe Email - ID" ForeColor="Red" SetFocusOnError="True" ValidateRequestMode="Enabled" ValidationExpression="^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
ValidationGroup="SignUp" Display="Dynamic"></asp:RegularExpressionValidator>
<asp:Label ID="Label17" runat="server"></asp:Label>
<br />
<br />
<div style="background-color: #D9853B">
<asp:Label ID="Label13" runat="server" Font-Bold="True" Font-Italic="True" Font-Size="Larger" Text="Password"></asp:Label>
<asp:TextBox ID="TextBox12" runat="server" class="input input-sm form-control" Font-Size="Larger" MaxLength="15" onblur="assign(this.id,HFpass)" placeholder="Enter your password here" TextMode="Password" ValidationGroup="SignUp"></asp:TextBox>
</div>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="TextBox12" ErrorMessage="*Password required" ForeColor="Red" ValidationGroup="SignUp" SetFocusOnError="True" ValidateRequestMode="Enabled" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server" ControlToValidate="TextBox12" ErrorMessage="Password length must be between 7 to 15 alphanumeric characters with @&#" ForeColor="Red" ValidationExpression="^[a-zA-Z0-9'@&#.\S]{7,15}$"
ValidationGroup="SignUp" SetFocusOnError="True" ValidateRequestMode="Enabled" Display="Dynamic"></asp:RegularExpressionValidator>
<br />
<br />
<div style="background-color: #D9853B">
<asp:Label ID="Label14" runat="server" Font-Bold="True" Font-Italic="True" Font-Size="Larger" Text="Confirm Password"></asp:Label>
<asp:TextBox ID="TextBox13" runat="server" class="input input-sm form-control" Font-Size="Larger" placeholder="Confirm your password" TextMode="Password" ValidationGroup="SignUp" ValidateRequestMode="Enabled"></asp:TextBox>
</div>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="TextBox13" ErrorMessage="*Confirm Password required" ForeColor="Red" ValidationGroup="SignUp" SetFocusOnError="True" ValidateRequestMode="Enabled" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server" BorderStyle="None" BorderWidth="2px" ControlToCompare="TextBox12" ControlToValidate="TextBox13" ErrorMessage="*Confirm password should match with password" ForeColor="Red" ValidationGroup="SignUp"
SetFocusOnError="True" ValidateRequestMode="Enabled"></asp:CompareValidator>
<br />
<br />
<div style="background-color: #D9853B">
<asp:Label ID="Label15" runat="server" Font-Bold="True" Font-Italic="True" Font-Size="Larger" Text="Contact number"></asp:Label>
<asp:TextBox ID="TextBox14" runat="server" class="input input-sm form-control" Font-Size="Larger" MaxLength="13" onblur="assign(this.id,HFphno)" placeholder="Enter your contact details here" TextMode="Phone" ValidationGroup="SignUp" ValidateRequestMode="Enabled"></asp:TextBox>
</div>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ControlToValidate="TextBox14" ErrorMessage="*Enter your contact number" ForeColor="Red" ValidationGroup="SignUp" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox14" ErrorMessage="*Enter number with (0 or +)&(91/22)& then the number" ForeColor="Red" SetFocusOnError="True" ValidateRequestMode="Enabled"
ValidationExpression="(((^[\+,0][9][1])(((\s[0-9]{7,10})|(\S[0-9]{7,10}))|([-]\S[0-9]{7,10})))|((^[\+,0][2]{2,2})((\S[0-9]{7,8})|((([-])[0-9]{7,8})|(\s[0-9]{7,8})))))|(((^[6,7,8,9][0-9]{9,9}))|(^[0,\+](([9][1)|[6,7,8,9]))[0-9]{8,9}))" ValidationGroup="SignUp"
Display="Dynamic" EnableViewState="False"></asp:RegularExpressionValidator>
<br />
<br />
<div style="background-color: #D9853B">
<asp:Label ID="Label16" runat="server" Font-Bold="True" Font-Italic="True" Font-Size="Larger" Text="User Type --->"></asp:Label>
<asp:RadioButton ID="RadioButton1" runat="server" Font-Size="Larger" GroupName="UserType" Text="Engineer" ValidationGroup="SignUp" />
<asp:RadioButton ID="RadioButton2" runat="server" Font-Size="Larger" GroupName="UserType" Text="Company" ValidationGroup="SignUp" />
</div>
<br />
<br />
<div style="background-color: #D9853B">
<asp:Button ID="SUSbmtBtn" runat="server" Font-Size="Larger" Height="49px" Text="Register" Width="125px" ValidationGroup="SignUp" class="btn btn-primary btn-lg" />
</div>
<br />
<br />
<div style="background-color: #FFF056">
<asp:Button ID="Button4" runat="server" Height="53px" OnClick="Button4_Click" Text="Login Here" Width="123px" ValidationGroup="ChangeView" class="btn btn-warning btn-lg" />
</div>
</div>
</asp:View>
</asp:MultiView>
</div>
</form>
<%-- <script type="text/javascript" src="Scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="Scripts/bootstrap.min.js"></script>--%>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="Scripts/MyJS.js"></script>
</body>
</html>
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.Text.RegularExpressions;
namespace Abhiyantrix
{
public partial class Login_Signup : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 0;
Button1.Enabled = false;
SUSbmtBtn.Enabled = false;
}
protected void Button3_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 1;
}
protected void Button4_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 0;
//MultiView1.SetActiveView(Login);
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source = OMKAR-PC ; Initial Catalog=ABHIYANTRIX PROJECT;Integrated Security = true;";
con.Open();
string query = "select * from Login where UserEmail = @Username and Password = @Password";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.Add(new SqlParameter("@Username", TextBox7.Text));
cmd.Parameters.Add(new SqlParameter("@Password", TextBox8.Text));
SqlDataReader DR = cmd.ExecuteReader();
if (DR.Read())
{
Session["username"] = DR["UserName"];
string cmpUsern = DR["UserType"].ToString();
/* if (cmpUsern.ToString() == (string)"Client")
{
Response.Redirect("ClientPage.aspx");
}
else if ((string)cmpUsern == "Engineer")
{
Response.RedirectPermanent("WebForm1.aspx");
}*/
if (cmpUsern.ToString() == (string)"Client")
{
Response.Redirect("ClientPage.aspx");
}
else if ((string)cmpUsern == "Engineer")
{
Response.Redirect("WebForm1.aspx");
}
}
else
{
Label4.Text = "Error";
}
con.Close();
}
catch (Exception E)
{
Label4.Text+= E.Message + " E.InnerException: " + E.InnerException;
//throw;
}
}
}
}
请帮助我,我已经添加了我认为需要的一切,如果您仍然需要一些东西,请询问。
注意:下面是我的注册视图
的屏幕截图 所有用红色写的第二个块都是我的正则表达式
答:
1赞
Julissa DC
2/17/2021
#1
我有同样的问题,这就是我解决它的方式。
您的 RegularExpressionValidator 具有 ,这意味着它不会在客户端运行,而是在服务器端运行(从代码中)。runat="server"
EnableClientScript="False"
您需要在代码中添加,特别是要重定向的位置,以防止向前移动并显示您在 RegularExpressionValidator 上设置的 。Page.IsValid
ErrorMessage
有关为什么 Page.isValid 会起作用的更多信息,请点击此处。
评论