javascript - 如何使用 Bootstrap 和 Angular 表单逐步验证表单?

标签 javascript node.js twitter-bootstrap angularjs

我找到这个例子step-by-step form with bootstrap and angularjs

如何在跳转到步骤 2 之前验证电子邮件? 或者阻止步进跳转直到字段填满?

function RegisterCtrl($scope, $location) {

  $scope.steps = [
    'Step 1: Team Info',
    'Step 2: Campaign Info',
    'Step 3: Campaign Media'
  ];

....some code

最佳答案

首先,在 Controller 中定义模型:

function RegisterCtrl($scope, $location) {
    $scope.step1 = {
        name: '',
        email: '',
        password: '',
        passwordc: ''
     };
    //...

将其绑定(bind)到您的表单字段:

<input type="text" id="inputEmail" ng-model="step1.email" placeholder="Email">

接下来,在 gotoStep() 中进行验证:

  $scope.goToStep = function(index) {
        if (!$scope.step1.email.match(/[a-z0-9\-_]+@[a-z0-9\-_]+\.[a-z0-9\-_]{2,}/)) {
              return window.alert('Please specify a valid email');
        }
        //...

显然警报不是很好,因此使用 jQuery focus() 并添加 Bootstrap 类(control-group warning)来突出显示该字段。

http://jsfiddle.net/xayzP/

关于javascript - 如何使用 Bootstrap 和 Angular 表单逐步验证表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14097851/

相关文章:

javascript - 如何处理大量项目的渲染 react ?

java - 在 Javascript 中动态设置复选框

node.js - SocketIO/NodeJS - 使用 socket.emit 时出错

javascript - Bootstrap offcanvas 侧边栏切换任何 Canvas 尺寸

javascript - React-Redux 可以不用react-saga 或react-thunk 框架吗?

javascript - 根据div高度展开div容器

regex - 文件名中包含空格的上传导致我的服务器出现错误

javascript - cheerio 使用 href 和 span 解析 h2

css - Bootstrap 4 标签不换行

jquery - 切换在 Angular ng-repeat 中无法正常工作