如何绑定输入类型='电子邮件'在knockoutjs中

how to bind input type= 'email' in knockoutjs

本文关键字:电子邮件 knockoutjs 类型 何绑定 绑定 输入      更新时间:2024-02-26

我将KnockoutJS与MVC结合使用。

我想知道emailId是有效还是无效,基于此,我必须启用/禁用按钮并为文本框设置错误标题。

这是我的.cshtml

<div class="nmc-righttab" style="width:265px;">
    <input class="nmc-text" type="email" maxlength="50" id="txtEmail" data-bind="value: emailAddress, valueUpdate: 'input'",css: { faultyBrush: (checkValidEmail() },attr: {title: InvalidEmailTitle } style="width:245px;" />
 </div> 
 <input type="submit" class="btn nmc-button" data-bind="click: searchUsers,enable: canSearchUsers,css: { 'btn-primary': (canSearchUsers() == true) }" id="searchUser" value="@Res.CommonStrings.Search" />

这是我的JS代码:

 this.emailAddress = ko.observable('');
 this.invalidEmailTitle = ko.observable('');
 this.canSearchUsers = ko.computed(function () {
     try {
         if (this.emailAddress().trim().length!==0)
             return true;
         else
              return false;
     } catch (error) {
         NMCApp.showNMCExceptionWindow('Jquery Error:' + error);
     }
}, this);
this.checkValidEmail(input) {
}

请让我知道如何知道电子邮件类型文本框的有效性,以及在Knockout JS中无效的情况下如何更改标题。

如何获取输入类型为"电子邮件"的文本框的有效性

如果您不想使用验证插件,您可以根据自己的regex:测试输入

HTML

<input data-bind="textInput: emailAddress, css: { 'faultyBrush': !checkValidEmail()}" class="nmc-text" type="email" maxlength="50" id="txtEmail" />

JS-

var self = this;
self.emailAddress = ko.observable('');
self.checkValidEmail = ko.pureComputed(function () {
    if (!self.emailAddress()) return false;
    var pattern = /REGEX-GOES-HERE/i;
    return pattern.test(self.emailAddress());
});

这里有几个模式。