1

我有一个单页应用程序。我需要在客户端验证我的字段。所以我使用了 knockout.js。我的视图页面上有两个字段,即电子邮件和密码。我想要什么,当我输入不正确格式的电子邮件 ID 然后移动到密码时,它应该给出电子邮件验证错误。简单来说,当输入格式错误的电子邮件 ID 并按 Tab 按钮时,我应该得到一个验证错误。我已经尝试过 'required pattern="@"' 进行验证,但是在完成 Ajax 调用后它不能正常工作。所以我打算用jquery来验证它。

 HTML:-
      <p>
   <label>email:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="email" name="email"  class="formElement" data-bind='value: email'
     required pattern="@" /></label></p>
     <p>
     <label>password:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="password"
      name="password" class="formElement" data-bind='value: password' /></label></p>

test.js :-

     ko.validation.rules.pattern.message = 'Invalid.';
     ko.validation.configure({
     registerExtenders : true,
     messagesOnModified : true,
     insertMessages : true,
     parseInputAttributes : true,
     messageTemplate : null
});

你可以在这里测试我的代码:- http://jsfiddle.net/kmvgw/324/

     var modalViewModel= {
     email : ko.observable().extend({ // custom message
     required : {
     message : 'Please type your  email address.'
     }
     }),
      password: ko.observable()
     };
     ko.applyBindings(modalViewModel,document.getElementById('light'));
4

2 回答 2

0

在这里,它的工作: -

email : ko.observable('').extend({ // 自定义消息 email: true }),

于 2012-11-30T04:28:19.237 回答
0
var User_Email_Id = $.trim($('#User_Email_Id').val());

if (! /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(User_Email_Id)){
  //error msg
    }

你可以试试这个

于 2012-11-29T11:04:10.473 回答