ASP.Net Webforms和validation

我有一个asp.net Web应用程序,我希望使用表单validationio jquery插件来执行客户端validation。

我已经设置了表单,并且插件在更改字段时validation字段,但是如果其中一个字段已经过validation,即使我有空的必填字段,它也允许提交表单的其余部分。

asp.net Web表单页面:

     

New User Form:

<label class="control-label col-sm-2" for="">Username:
<label class="control-label col-sm-2" for="">Email:
<label class="control-label col-sm-2" for="">Password:
<label class="control-label col-sm-2" for="">Confirm Password:

$(document).ready(function() { $('#NewUserForm').formValidation({ framework: 'bootstrap', icon: { valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' }, fields: { // There is no single quote : { validators: { notEmpty: { message: 'The username is required and cannot be empty' }, different: { field: '', message: 'The username and password cannot be the same as each other' } } }, : { validators:{ notEmpty: { Message: 'The email field is required and cannot be empty' }, email: { Message: 'Please enter a valid email address!' } } }, : { validators:{ notEmpty: { Message: 'A password is required and cannot be empty' }, stringLength: { min: 7, max: 30, message: 'The password must be more than 7 and less than 30 characters long' }, regexp: { regexp: /^(?=.*?[AZ])(?=.*?[az])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/, message: 'Password must contain at least 1 uppercase, 1 lowercase, 1 number and 1 special character' }, identical: { field: '', message: 'Passwords do not match' } } }, :{ validators:{ identical: { field: '', message: 'Passwords do not match' } } } } }); });

有关如何在提交之前让页面validation所有控件的任何建议 – 以及是否有任何失败的validation以防止表单提交。

我使用的插件是: http : //formvalidation.io/

OnClientClick属性是您需要的。 将其添加到您的提交按钮并指定一个Javascript函数来执行您的validation表单。 如果此函数返回true ,则表单将正常提交,如果为false ,则将阻止提交。

对于formvalidation.io( 来自API文档 )的情况,您可以执行以下操作:

  ...  

这里最重要的是确保你在函数和OnClientClick属性中都return – 否则它将不起作用!