【发布时间】:2014-06-14 18:19:59
【问题描述】:
基本上,我在表单上运行 bootstrapvalidator,它似乎在决定自己验证什么(以及如何验证)。虽然我知道这在技术上不是它在做什么,但它确实感觉像它。
我的表格:
<form class="form-horizontal" role="form" id="regcv" enctype="multipart/form-data">
<div class="col-md-3">
<div class="form-group">
<label for="firstname" class="control-label">First name</label>
<input type="text" class="form-control" id="firstname" name="firstname" placeholder="First name">
</div>
<div class="form-group">
<label for="lastname" class="control-label">Last name</label>
<input type="text" class="form-control" id="lastname" name="lastname" placeholder="Last name">
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Email address">
</div>
<div class="form-group">
<label for="verifyemail" class="control-label">Verify email</label>
<input type="email" class="form-control" id="verifyemail" name="verifyemail" placeholder="Email verification">
</div>
<div class="form-group">
<label for="phone" class="control-label">Phone</label>
<input type="text" class="form-control" id="phone" name="phone" placeholder="+47 00 00 00 00">
</div>
</div>
<div class="col-md-offset-1 col-md-3">
<div class="form-group">
<label for="freetext" class="control-label">Free text (<small>optional</small>)</label>
<textarea class="form-control" id="freetext" rows="16" name="freetext" placeholder="Anything you feel would be applicable, a full resume or similar"></textare
a>
</div>
</div>
<div class="col-md-offset-1 col-md-3">
<div class="form-group">
<label for="cv">Upload CV</label>
<input type="file" id="cv" name="cv[]" accept="application/pdf">
<p class="help-block">Accepted fileformats: .pdf.</p>
</div>
<div class="form-group">
<label for="certs">Upload certificates</label>
<input type="file" id="certs" name="certs[]" accept="application/pdf">
<p class="help-block">Accepted fileformats: .pdf.</p>
</div>
<div class="form-group">
当表单如上所示时,它会正确验证前四个字段(名字、姓氏、电子邮件和验证电子邮件),但会禁用提交按钮而不将任何内容标记为无效。但是,如果我将“电话”字段更改为 type=“电子邮件”,我可以通过点击“提交”来强制通过,而该字段会得到验证,我可以再次单击“提交”并实际发送数据。由于某些奇怪的原因,它还更改了表单并添加了 data-bv-field='phone'。
我的 JS:
$('#regcv').bootstrapValidator({
fields: {
firstname: {
validators: {
notEmpty: {
message: 'Required field'
}
}
},
lastname: {
validators: {
notEmpty: {
message: 'Required field'
}
}
},
email: {
validators: {
notEmpty: {
message: 'Required field'
},
emailAddress: {
message: 'The input is not a valid email address'
}
}
},
verifyemail: {
validators: {
notEmpty: {
message: 'Required field'
},
identical: {
field: 'email',
message: 'Email does not match'
}
}
},
},
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
submitHandler: function (validator, form, submitButton) {
var form = new FormData($('#regcv')[0]);
$.ajax({
url: 'ajax/action.php',
type: 'POST',
xhr: function () {
var myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) {
myXhr.upload.addEventListener('progress', progress, false);
}
return myXhr;
},
success: function (res) {
$('#content_here_please').html(res);
},
error: function (request, error) {
alert(" Can't do because: " + error);
},
data: form,
cache: false,
contentType: false,
processData: false
});
//$("#regcv :input").attr('disabled', true);
}
});
JSFiddle: http://jsfiddle.net/nz7ej/
任何帮助将不胜感激。
编辑: 忘了提,但是删除字段本身(电话字段)没有帮助,表单仍然不会验证,即使存在对该字段的零引用,并且所有当前字段都是有效的。
【问题讨论】:
标签: jquery html ajax twitter-bootstrap validation