我的表单中的自定义验证程序无效,请查看下面的javascript和代码
<script type="text/javascript">
function checkalphabets(sender, args) {
var pattern = /^[a-zA-Z\s]+$/;
var regexp1 = new RegExp("(www|http|https|WWW|HTTP|HTTPS)");
var sUser = <%= CreateFirstNameTextBox.ClientID %>.value;
if ((!pattern.test(args.value))|| (regexp1.test(args.value)))
{
return false;
}
return true;
}
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="CreateFirstNameTextBox" placeholder="First Name"
MaxLength="50"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator2" runat="server" ErrorMessage="Only Alphabets allowed! & http, www, https are not allowed"
ClientValidationFunction="checkalphabets" ForeColor="red" ControlToValidate="CreateFirstNameTextBox"
ValidationGroup="create" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" ValidationGroup="create" />
</div>
</form>
我必须只允许字母表使用文本框,不应允许使用www,http,https
答案 0 :(得分:0)
你需要在MSDN中看到关于ClientValidationFunction的示例,你可以看到在验证函数中你需要为参数设置IsValid
属性,所以你需要改变你的代码,如
function checkalphabets(sender, args) {
var pattern = /^[a-zA-Z\s]+$/;
var regexp1 = new RegExp("(www|http|https|WWW|HTTP|HTTPS)");
var sUser = args.Value; //<%= CreateFirstNameTextBox.ClientID %>.value;
args.IsValid = pattern.test(sUser);
}
另请<%= CreateFirstNameTextBox.ClientID %>.value
更好地使用args.Value
更新以同时使用条件
function checkalphabets(sender, args) {
var pattern = /^[a-zA-Z\s]+$/;
var regexp1 = new RegExp("(www|http|https|WWW|HTTP|HTTPS)");
var sUser = args.Value; //<%= CreateFirstNameTextBox.ClientID %>.value;
args.IsValid = pattern.test(sUser) && !regexp1.test(sUser);
}