【问题标题】:Bootstrapvalidator validating fields without being told soBootstrapvalidator 验证字段而不被告知
【发布时间】: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


    【解决方案1】:

    我能够通过将电话输入字段类型改回“文本”并在 JS 验证器列表中添加电话验证器来使验证工作:

                phone: {
                    validators: {
                        notEmpty: {
                            message: 'Required'
                        },
                        phone: {
                            country: 'US',
                            message: 'Enter a valid phone number'
                        }
                    }
                },
    

    根据您的占位符文本,您似乎想要验证非美国号码。请参阅此图表以查看将哪个国家/地区代码传递给电话验证器:

    https://www.iso.org/obp/ui/#search

    虽然看起来当前的 bootstrapvalidattor 只适用于美国和英国的电话号码:

    http://bootstrapvalidator.com/validators/phone/

    最后,请注意 JSFiddle 工具栏上的 TidyUp 按钮——它使您的代码更具可读性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 2020-03-20
      • 1970-01-01
      相关资源
      最近更新 更多