Javascript 点击敲除js中的按钮时触发验证

Javascript 点击敲除js中的按钮时触发验证,javascript,knockout.js,knockout-validation,Javascript,Knockout.js,Knockout Validation,我有一个表单使用KOJs进行计算,我需要在那里实现KOJs验证,这样用户就不应该在表单中提交空白字段 我已经尝试了各种各样的资源,但都没有什么帮助 你们能解决这个问题吗。 任何帮助都将不胜感激 下面是我的代码 function ViewModel() { this.fullname = ko.observable().extend({ required: true, minLength: 2, maxLength: 10 });

我有一个表单使用KOJs进行计算,我需要在那里实现KOJs验证,这样用户就不应该在表单中提交空白字段

我已经尝试了各种各样的资源,但都没有什么帮助

你们能解决这个问题吗。 任何帮助都将不胜感激

下面是我的代码

function ViewModel() {

                this.fullname = ko.observable().extend({ required: true, minLength: 2, maxLength: 10 });

                this.startDate = ko.observable(new Date());

                this.companyname = ko.observable().extend({ required: true });

                this.loanAmount = ko.observable();

                this.email = ko.observable();

                this.phoneNumber = ko.observable();

                this.sellerName = ko.observable();

                this.propertyAddress = ko.observable();

                this.propertyCity = ko.observable();

                this.mortgagePayoff = ko.observable();

                this.realtorCommission = ko.observable('6');

                this.termitePolicy = ko.observable();

                this.closingCosts = ko.observable();

                this.additionalNote = ko.observable();

                this.PropertyCity = ko.observable();

                this.selectedcounty = ko.observable();

                this.no_documents = ko.observable();
                this.fileInput =  ko.observable();
                this.fileName = ko.observable();




                this.submitForm = function()

                {

                    var data = JSON.stringify(

                        {

                            fullname : this.fullname(), 

                            companyname : this.companyname(),

                            email: this.email(), 

                            phoneNumber: this.phoneNumber(),

                            sellername: this.sellerName(),

                            propertyAddress: this.propertyAddress(),

                            propertycity: this.propertyCity(),

                            county: this.selectedcounty(),

                            contractSalesPrice: this.contractSalesPrice(),

                            selectedPropertyType: this.selectedPropertyType(),

                            loanAmount : this.loanAmount(),

                            wireFee : this.wireFee(),

                            mortgagePayoff: this.mortgagePayoff(),

                            realtorCommission: this.realtorCommission(),

                            revenueStamps: this.revenueStamps(),

                            termitePolicy: this.termitePolicy(),

                            closing_fee : this.closing_fee(),

                            title_service_fee: this.title_service_fee(),

                            titleInsurance: this.titleInsurance(),

                            ownersPremium: this.ownersPremium(),

                            loanPremium: this.loanPremium(),

                            comboPrice: this.comboPrice(),


                            no_documents: this.no_documents(),

                            courierFee: this.courierFee(),

                            homeWarranty: this.HomeWarranty(),

                            priorYear: this.PriorYear(),

                            startDate: this.dateformate(),

                            proRatedTaxes: this.ProRatedTaxes(),
                            insured_closing: this.insured_closing(),
                            RecordingFees: this.RecordingFees(),
                            closingCosts: this.closingCosts(),
                            additionalNote: this.additionalNote(),
                            fileName: this.fileName(),
                            fileInput: this.fileInput()


                        });


                    jQuery.post("../view.php", {json:data}, function(response)

                    {
                        //alert(response);
                    window.location.href = 'http://realtytitle.madeby.ws/result/';
                       // on success callback
                        //this.response(response);

                    })

                } 
            }

            ko.applyBindings(new ViewModel());

首先,您不需要生成数据对象。Knockout有一个很好的函数:ko.toJSON()。在ViewModel的第一行添加如下变量:

var self = this;
这样,您就可以从任何函数访问viewModel对象。然后,在submitForm上刚刚设置

var data = ko.toJS(self);
关于验证,一个简单但不太优雅的解决方案是创建一个验证数据的函数,如:

    this.isValid = function() {
     return self.fullname().length > 0 && 
            self.email().length > 0 &&
            ... && the validations you want.
    }
此函数可用于验证submitForm上的对象。另一种方法是使用customHandler来验证每个可观察对象,如

this.email = ko.observable(); 
this.email.valid = ko.computed() { 
  if (self.email())
    return self.email().length > 0;
  return false;
}
其他元素也是如此。然后,只需在submitForm函数上验证self.email.valid()。另外,这允许您在用户界面上显示元素是否有效。最后,你可以使用一个插件。正如你所看到的,有很多的可能性