我找到了这个带有 bootstrap 和 angularjs 的示例分步表单
在跳转到第 2 步之前如何验证电子邮件?或阻止步骤跳转,直到字段已满??
function RegisterCtrl($scope, $location) {
$scope.steps = [
'Step 1: Team Info',
'Step 2: Campaign Info',
'Step 3: Campaign Media'
];
....some code
我找到了这个带有 bootstrap 和 angularjs 的示例分步表单
在跳转到第 2 步之前如何验证电子邮件?或阻止步骤跳转,直到字段已满??
function RegisterCtrl($scope, $location) {
$scope.steps = [
'Step 1: Team Info',
'Step 2: Campaign Info',
'Step 3: Campaign Media'
];
....some code
您应该使用指令来测试您的表单字段的有效性,例如:
app.directive('email', function() {
return {
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
ctrl.$parsers.unshift(function(viewValue) {
if (viewValue && viewValue.match(/[a-z0-9\-_]+@[a-z0-9\-_]+\.[a-z0-9\-_]{2,}/)) {
// it is valid
ctrl.$setValidity('email', true);
return viewValue;
} else {
// it is invalid, return undefined (no model update)
ctrl.$setValidity('email', false);
return undefined;
}
});
}
};
});
在您的 html 中,您需要将指令添加到您的输入字段。如果使用对象的字段无效,您可以显示错误消息myForm.email.$error
:
<input type="text" name="email" id="inputEmail" placeholder="Email" ng-model="email" email required>
<span ng-show="myForm.email.$error.email" class="help-inline">Email invalid</span>
<span ng-show="myForm.email.$error.required" class="help-inline">Email required</span>
您可以通过在ng-class上使用myForm.$invalid来禁用下一个链接,直到表单有效:
<li ng-class="{disabled: myForm.$invalid}" >
<a ng-model="next" ng-click="incrementStep(myForm)">Next Step →</a>
</li>
见例子。
首先,在控制器中定义模型:
function RegisterCtrl($scope, $location) {
$scope.step1 = {
name: '',
email: '',
password: '',
passwordc: ''
};
//...
将其绑定到您的表单字段:
<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');
}
//...
显然 alert 不是很好,所以使用 jQueryfocus()
并添加 Bootstrap 类 ( control-group warning
) 以突出显示该字段。