gpt4 book ai didi

knockout.js - Knockout 验证插件 - 仅在表单提交时显示错误消息

转载 作者:行者123 更新时间:2023-12-04 03:24:02 25 4
gpt4 key购买 nike

我正在使用 knockout 绑定(bind)和 knockout 验证插件( https://github.com/Knockout-Contrib/Knockout-Validation )

我只想在表单提交时显示错误消息,而不是在输入模糊中,有什么想法吗?

HTML

<div id="formErrorMessages" data-bind="visible: errors().length != 0">
<p data-bind="validationMessage : email"></p>
<p data-bind="validationMessage: firstName"></p>
<span class="arrow-down"></span> </div>

<form id="short-reg-form" method="post" data-bind="validationOptions: { insertMessages:false,decorateInputElement:true,errorElementClass:'input-error' },submit:submit">
<input type="text" name="email" data-bind="value: email" class="email" data-placeholder="Email"/>
<input type="text" name="firstName" data-bind="value: firstName" class="right-input" data-placeholder="First Name"/>

</form>

JS

function viewModel() {

var self = this;

self.showErrorsDiv = ko.observable(false);
//Email
var emailPlaceholder = getPlaceholder("email");
self.email = ko.observable("")
.extend({
ng_required: {
params: emailPlaceholder,
message: notValidValueReturnString(emailString)
},
ng_email: {
params:emailPlaceholder,
message:notValidValueReturnString(emailString)
}
}).clearError(); // invoke clearError to prevent validation on page load.
//--Email
//FirstName
var firstNamePlaceholder = getPlaceholder("firstName");
self.firstName = ko.observable("").extend({
ng_required: {
params: firstNamePlaceholder,
message : notValidValueReturnString(firstNameString)
}
});
//--FirstName
self.submit = function (e) {

if (self.errors().length == 0) { // check form validation.
alert(true);
} else {
alert(false);
}

//prevent form submit default behavior.
return false;
};

// utilities
function getPlaceholder(nameAttributeVal) {
return $('#short-reg-form input[name="' + nameAttributeVal + '"]').attr("data-placeholder");
}//--utilities

}

$(function () {

// attach view model to the DOM
var pageModel = new viewModel();
pageModel.errors = ko.validation.group([pageModel.email,pageModel.firstName]);

ko.applyBindings(pageModel);

});

谢谢。

最佳答案

我也有这个要求 - 客户不希望用户在实际尝试提交表单之前看到“红色”。查看此代码笔 - http://codepen.io/dmoojunk/pen/zxqYbb

HTML-

<div class="container no-padding">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="col-md-4 col-sm-4 col-xs-12">
<div>
<div>
<p>Phone: <a href="tel:0123456789">0123456789</a></p>
<p>Email: <a href="mailto:a@b.com" target="_top">email@example.co.uk</a></p>
<pre data-bind="text: ko.toJSON($data, null, 2)"></pre>
</div>
</div>
</div>

<div class="col-md-8 col-sm-8 col-xs-12">
<div>
<div>
<form role="form">
<div class="row">
<div class="col-md-6">
<div class="form-group" data-bind="validationElement: firstName">
<label class="control-label" for="contact-name">First Name</label>
<input type="text" class="form-control" id="contact-name" placeholder="i.e Joe" data-bind="textInput: firstName"/>
<span class="help-block" data-bind="validationMessage: firstName"></span>
</div>
</div>
<div class="col-md-6">
<div class="form-group" data-bind="validationElement: email">
<label class="control-label" for="contact-email">Email</label>
<input type="email" class="form-control" id="contact-email" placeholder="i.e email@info.com" data-bind="textInput: email"/>
<span class="help-block" data-bind="validationMessage: email"></span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group" data-bind="validationElement: surname">
<label class="control-label" for="contact-surname"> Surname</label>
<input type="text" class="form-control" id="contact-surname" placeholder="i.e Bloggs" data-bind="textInput: surname"/>
<span class="help-block" data-bind="validationMessage: surname"></span>
</div>
</div>
<div class="col-md-6">
<div class="form-group" data-bind="validationElement: category">
<label class="control-label" for="contact-topic">Please select a category</label>
<select class="form-control" id="contact-topic" data-bind="value: category">
<option value="">Select category</option>
<option value="general">General contact</option>
<option value="issue">Report an issue</option>
<option value="help">Help enquiry</option>
</select>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group" data-bind="validationElement: message">
<label class="control-label" for="contact-textarea">Write your message here, the more detail the better</label>
<textarea style="width:100%" id="contact-textarea" placeholder="Place message here" rows="10" data-bind="textInput:message"></textarea>
<span class="help-block" data-bind="validationMessage: message"></span>
</div>
<button type="button" class="btn btn-green" data-bind="click: submit">Get in touch</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>

和 JS -

var mynamespace = mynamespace || {};

ko.validation.init({
errorElementClass: 'has-error',
errorMessageClass: 'help-block',
decorateElement: true,
insertMessages: false
});

//Viewmodel
mynamespace.ContactUsViewModel = function(){
var self = this;
self.validationEnabled = ko.observable(false);
self.isValidationEnabled = function() {
return self.validationEnabled();
}
self.firstName = ko.observable('').extend({
required: {
onlyIf: self.isValidationEnabled
},
minLength: {
onlyIf: self.isValidationEnabled,
params: 5
}
});
self.email = ko.observable('').extend({
required: {
onlyIf: self.isValidationEnabled
},
email: {
onlyIf: self.isValidationEnabled
}
});
self.surname = ko.observable('').extend({
required: {
onlyIf: self.isValidationEnabled
},
minLength: {
onlyIf: self.isValidationEnabled,
params: 5
}
});
self.category = ko.observable('').extend({
required: {
onlyIf: self.isValidationEnabled
},
minLength: {
onlyIf: self.isValidationEnabled,
params: 1
}
});
self.message = ko.observable('').extend({
required: {
onlyIf: self.isValidationEnabled
},
minLength: {
onlyIf: self.isValidationEnabled,
params: 5
}
});
self.submit = function() {
self.validationEnabled(true);
if (!this.isValid()) {
this.errors.showAllMessages();
} else {
alert('Form Valid');
};
}
};

var viewmodel = ko.validatedObservable(new mynamespace.ContactUsViewModel())();
ko.applyBindings(viewmodel);

关于knockout.js - Knockout 验证插件 - 仅在表单提交时显示错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24301286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com