AngularJS登录验证错误信息不显示问题求助
Fixing Your AngularJS Login Validation Issue
Hey Krish, let's walk through what's causing your login form to skip validation and jump straight to next.html—it's a mix of typos and incorrect setup. Here's the breakdown and fixes:
Key Issues in Your Code
- Angular Module Typo: You wrote
angular.modul("demoApp",[])instead ofangular.module("demoApp",[])(missing the final "e" in "module"). This breaks your controller entirely, so your validation function never runs. - Misspelled
length: Both validation checks useleggthinstead oflength. JavaScript doesn't recognize this property, so your error messages never trigger, ande.preventDefault()never stops the form submission. - Invalid Script Tags: Your script tags have
typeset to file paths (liketype="lib/angular.min.js"), which is wrong. You need to use thesrcattribute to load external scripts. - Unnecessary Form Action: Keeping the
actionattribute means the browser will fall back to it if your JS fails, which is why it jumps tonext.htmleven with validation issues. - Missing Closing Form Tag: Your original HTML didn't close the
<form>element, which can cause unexpected browser behavior.
Corrected Code
index.html
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Login Form</title> <!-- Fixed script tags: use src instead of invalid type attributes --> <script src="lib/angular.min.js"></script> <script src="angularFile.js"></script> </head> <body ng-app="demoApp" ng-controller="Ctrl"> <h1>Login Form</h1> <!-- Removed action attribute to let Angular handle submission --> <form ng-submit="fun1($event)"> userName:<input name="name" ng-model="un"> <div style="color: red">{{msg1}}</div><br> passWord:<input type="password" name="pwd" ng-model="pw"> <div style="color: red">{{msg2}}</div><br> <input type="submit" value="Login"/> </form> <!-- Added missing closing form tag --> </body> </html>
angularFile.js
// Fixed module name typo var app = angular.module("demoApp",[]); app.controller("Ctrl",function($scope){ $scope.un=""; $scope.pw=""; $scope.msg1=""; $scope.msg2=""; $scope.fun1=function(e){ // Reset error messages on each submission $scope.msg1 = ""; $scope.msg2 = ""; var isFormValid = true; // Fixed length spelling for username check if($scope.un.length === 0){ $scope.msg1 = "please enter user name"; isFormValid = false; } // Fixed length spelling for password check if($scope.pw.length === 0){ $scope.msg2 = "please enter password"; isFormValid = false; } // Only prevent form submission if there are errors if(!isFormValid){ e.preventDefault(); } else { // Redirect programmatically when validation passes window.location.href = "next.html"; } } });
What Changed?
- Fixed script loading to use
srcinstead of invalidtypeattributes, ensuring Angular and your controller script load correctly. - Removed the form's
actionto avoid browser fallback submission, so we have full control via Angular. - Corrected typos that broke your Angular controller and validation checks.
- Improved validation logic to reset messages on each submit, track form validity, and use programmatic redirect for better control.
- Added the missing closing
<form>tag to fix HTML structure issues.
Test this out, and your error messages should now show up when fields are empty, and the form will only redirect to next.html when both fields are filled.
内容的提问来源于stack exchange,提问作者Krish
相关产品推荐
相关产品推荐

