我在 bootstrap asp.net 项目中使用http://bootstrapvalidator.com/和 Visual Studio。我遇到的问题是,我永远无法像在示例中那样实际验证按钮,当我开始输入文本框时它验证良好,但当我单击示例后的按钮时从未验证。
另一个问题是我不知道如何调用我的 c# 方法,该方法将在验证成功后从文本框中获取值,我可以在按钮单击时调用我的方法,但我希望它在验证成功后发生。
我正在寻找的是简单的示例,如何使提交按钮验证字段,然后调用我的 c# 方法,该方法将从输入中读取值。
我什至无法在网上找到单个示例,而且我对 javascript 了解不多。
这是我的一些测试代码
<div class="form-group" id="nameEmail2">
<div class="form-group">
<label for="inputContactName" class="col-sm-5 control-label">Contact Name </label>
<div class="col-sm-7">
<input type="text" class="form-control" name="fullName1" id="TextBox1" placeholder="Contact Name" maxlength="50"/>
</div>
</div>
<div class="form-group">
<label for="inputContactEmail" class="col-sm-5 control-label">Contact Email <b>*</b></label>
<div class="col-sm-7">
<input type="text" class="form-control" name="email1" id="TextBox2" placeholder="Contact Email" maxlength="50"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-5 col-sm-offset-3">
<button type="submit" runat="server" onserverclick="submitValidate" class="btn btn-default">Validate</button>
</div>
</div>
</div>
<script type="text/javascript">
$(function () {
$('#nameEmail2').bootstrapValidator({
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
fullName1: {
validators: {
notEmpty: {
// enabled is true, by default
message: 'The full name is required and cannot be empty'
},
stringLength: {
enabled: true,
min: 8,
max: 40,
message: 'The full name must be more than 8 and less than 40 characters long'
},
regexp: {
enabled: false,
regexp: /^[a-zA-Z\s]+$/,
message: 'The full name can only consist of alphabetical, number, and space'
}
}
},
email1: {
validators: {
emailAddress: {
message: 'The value is not a valid email address'
}
}
}
}
});
});
</script>
我已经删除了大部分 asp 对象,几乎没有任何东西在服务器上运行,但这并没有多大帮助,这是我的代码隐藏方法
protected void submitValidate(object sender, EventArgs e)
{
string contactName = Request.Form["fullName1"];
string email = Request.Form["email1"];
System.Diagnostics.Debug.Write("test",email);
}