8 QTN
8 QTN
8 QTN
Username<br>
<input type="text" name="username" required> <br>
Password <br>
<input type="password" name="password" required> <br>
<button type="submit">Login</button>
</form>
The `<input>` tag in HTML can take various attributes depending on the type of input and
the desired behaviour. Here are some common attributes used with the `<input>` tag:
1. type:
- Specifies the type of input. For example, `text` for plain text, `password` for password
input, `checkbox` for checkboxes, `radio` for radio buttons, etc.
```html
<input type="text">
```
2. name:
- Provides a name for the input field. This name is used when submitting the form, and it's
essential for identifying the data on the server.
```html
<input type="text" name="username">
3. placeholder:
- Displays a short hint that describes the expected value of the input field. It is typically
used for providing an example or guidance to the user.
```html
<input type="text" name="username" placeholder="Enter your username">
4. required:
- Indicates that the input field must be filled out before submitting the form.
```html
<input type="text" name="username" required>
```
`ng-submit` is an AngularJS directive used in HTML forms to specify the behaviour that
should occur when the form is submitted. It is primarily used to bind a function or expression
defined in the AngularJS controller to the form submission event.
Explanation of `ng-submit`:
Purpose:
- Form Submission Handling: `ng-submit` is designed to handle the form submission event in
AngularJS applications.
Syntax:
```html
<form ng-submit="expression">
<!-- Form elements go here -->
</form>
```
How it works:
```javascript
if ($scope.username.toLowerCase() === 'viraj' && $scope.password === '123') {
alert('Login successful');
// Add further logic for successful login
}
```
Explanation:
1. `$scope` Object:
- In AngularJS, the `$scope` object is a special object that acts as a bridge between the
controller and the view. It allows data binding, meaning changes in the model (data) are
immediately reflected in the view, and vice versa.
3. `toLowerCase()`:
- The `toLowerCase()` method is used on `$scope.username`. This method converts the
string to lowercase. In this context, it ensures a case-insensitive comparison when checking if
the entered username is 'viraj'. Without `toLowerCase()`, the comparison would be case-
sensitive.
```javascript
$scope.username.toLowerCase() === 'viraj'
```
```javascript
$scope.password === '123'
```
5. Condition:
- The `if` statement checks if both conditions are true. If the entered username is 'viraj'
(case-insensitive) and the password is '123', then the condition evaluates to true.
```javascript
if ($scope.username.toLowerCase() === 'viraj' && $scope.password === '123')
```
```javascript
alert('Login successful');
```
```javascript
alert(message);
```
```javascript
if ($scope.username.toLowerCase() === 'viraj' && $scope.password === '123') {
alert('Login successful');
// Add further logic for successful login
} else {
alert('Login failed. Invalid username or password.');
// Add logic for failed login
}
```
- If the condition (successful login) is met, an alert box with the message `'Login successful'`
will be displayed.
- If the condition is not met (failed login), an alert box with the message `'Login failed.
Invalid username or password.'` will be displayed.
In a real-world application, you might use more sophisticated methods to communicate with
the user, such as updating the UI dynamically, displaying error messages on the page, or
redirecting to different pages. The `alert` function, however, provides a quick and simple way
to show messages in a pop-up dialog.
```javascript
$scope.login = function () {
// Login logic goes here
};
```
2. `ng-submit="login()"`:
- In the associated HTML form, the `ng-submit` directive is used to bind a function to the
form submission event.
- `ng-submit="login()"` specifies that when the form is submitted, the function referred to
as `login` on the `$scope` object should be executed.
```html
<form ng-submit="login()">
<!-- Form elements go here -->
</form>
```
This approach allows you to encapsulate login-related logic within the controller and trigger
it seamlessly from the view when needed.
-----------------------example -----------------------------------------------------------------------
Relation between $scope.login = function () & ng-submit="login ()"
```javascript
$scope.login = function () {
// Set of instructions for logging in
};
```
2. `ng-submit="login()"`:
- Imagine you have a magic button on a form. When you press this button, you want
something special to happen, like logging in.
- `ng-submit` is like a spell you put on this magic button. You tell the button, "Hey, when
someone presses you, perform the magic associated with the `login` function from our
backpack."
```html
<form ng-submit="login()">
<!-- Form elements go here -->
</form>
```
Therefore, the form submission is like saying, "Hey magic button, cast the spell to execute
the login instructions from the backpack!"