如果电话号码已注册,则显示错误消息
Showing error message if phone number is already registered
我是Angular.js的新手,正在为应用程序开发注册页面。这里我把用户的电话号码作为主键。在用户输入他的电话号码后的注册表中,我正在对数据库进行AJAX调用,以检查号码是否已经存在。我成功地完成了那部分。但是如果用户已经注册,我不知道如何向用户显示错误消息。
我的index.html 代码
<div class="form-group" ng-class="{ 'has-error' : userForm.contactno.$invalid && (userForm.contactno.$dirty || submitted) }">
<label>ContactNo</label>
<input phone type="text" name="contactno" class="form-control" ng-model="user.phone" placeholder="Your Contact No" ng-pattern="/^[789]'d{9}$/" maxlength="10">
<p ng-show="userForm.contactno.$error.pattern && (userForm.contactno.$dirty || submitted)" class="help-block">Enter a valid contactno.</p>
</div>
这是我的AJAX调用:
myAppDirectives.
directive('phone', function() {
return {
restrice: 'A',
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
angular.element(element).bind('blur', function() {
var phnNum = this.value;
$.ajax({
url: 'http://localhost:8087/users/'+phnNum,
dataType: 'application/json',
type: 'GET',
success: function( data, status, headers, cfg ){
console.log("data" +data);
},
error: function( jqXHR, textStatus, errorThrown ){
if(result.responseText.length===0){
console.log("you can register now");
}
else{
console.log("User with this number is already registered");
}
}
});
});
}
提前感谢
在页面中显示错误消息非常容易。只需将错误消息传递到scope并将其显示在相关页面中即可。就像这样。Ajax的Insted在您的angular请求中练习使用$http.post或$http.get。
$scope.checkNumber=function(phnNum){
$http.post('http://localhost:8087/users', phnNum).success(function(data){
if(data == "false"){
$scope.error = 'User with this number is already registere.';
} else {
$scope.submitTrueMessage = 'you can register now';
$scope.error = null;
}
});
};
这就是如何在视图中显示错误消息。
<span style="color:red"> {{error}} </span>
希望这些信息对你有帮助。
如果您想在数据库中存在电话号码时显示警报,则应尝试在api中进行编码。
您可以在回调函数中编写逻辑,即"success"&"错误"。
或者,您也可以使用Angular JS中的ngMessages模块来设计自定义错误消息。
docs.angularjs.org/api/ngMessages
检查电话号码,如果它已经通过数据库中的循环退出,则使用
{{error}}并在if部分和else部分中定义代码部分中的错误以存储电话号码。
相关文章:
- ng消息仅在触摸时显示错误,并在错误的初始显示上转换
- ui网格日期单元格过滤器,过滤日期格式导致显示错误的日期
- java脚本的数学方程显示错误的答案
- 如果用户输入的长度小于最小长度,则显示错误消息
- Push方法显示错误
- Javascript设置日期不起作用,显示错误的时间
- 显示错误的Ajax调用无法调试
- 显示错误结果的Javascript
- 分页逻辑显示错误的页码
- 如何显示错误消息
- 表单提交没有'如果为空,则不会显示错误消息
- 使用RecordRTC录制的WebRTC视频在IE和Safari中显示错误
- 如何仅在存在最小值和最大值时才显示错误消息
- AngularJS错误处理:根据错误数组显示错误
- PHP 显示错误的名称值
- 在 api 调用$resource显示错误和成功消息
- 为什么 Angularjs 总是显示错误消息
- 在单选按钮验证中同时显示错误消息
- jQuery'摇动'功能显示错误
- Jquery添加更多和删除冲突并显示错误值