javascript notes2
javascript notes2
A regular expression is a character sequence defining a search pattern. It’s
employed in text searches and replacements, describing what to search for within a
text. Ranging from single characters to complex patterns, regular expressions
enable various text operations with versatility and precision.
A regular expression can be a single character or a more complicated pattern.
Syntax:
/pattern/modifiers;
Example:
let patt = /GeeksforGeeks/i;
Explanation :
/GeeksforGeeks/i is a regular expression.
GeeksforGeeks is the pattern (to be used in a search).
i is a modifier (modifies the search to be Case-Insensitive).
Regular Expression Modifiers can be used to perform multiline searches which
can also be set to case-insensitive matching:
Expressions Descriptions
\d Find a digit
JavaScript errors and validation are crucial for creating robust web
applications. Here's a breakdown:
Errors in JavaScript
Types of Errors:
Syntax Errors: These occur when you write incorrect JavaScript code, like missing
semicolons or incorrect syntax.
Reference Errors: These happen when you try to access a variable that hasn't been
declared or is out of scope.
Type Errors: These occur when you perform an operation on a value of the wrong
type, like trying to add a number to a string.
Range Errors: These happen when you provide an argument to a function that's
outside the allowed range.
URI Errors: These occur when you use incorrect syntax with functions
like encodeURI() or decodeURI().
Handling Errors:
try...catch: This statement allows you to catch and handle errors gracefully.
JavaScript
try {
// Code that might throw an error
} catch (error) {
// Code to handle the error
console.error(error.message);
}
throw: You can use the throw statement to create custom errors.
JavaScript
if (x < 0) {
throw new Error("x cannot be negative");
}
Validation in JavaScript
Purpose:
Validation ensures that user input or data meets specific criteria before it's
processed.
Common Validation Tasks:
Checking if a field is empty.
Checking if an email address is valid.
Checking if a password meets certain criteria (length, special characters, etc.).
Checking if a number is within a certain range.
Methods for Validation:
HTML5 Constraint Validation: HTML5 provides built-in validation attributes for form
elements.
Code
<input type="email" required>
JavaScript Validation: You can write custom JavaScript code to validate input
fields.
JavaScript
function validateForm() {
let email = document.getElementById("email").value;
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
alert("Please enter a valid email address.");
return false;
}
return true;
}
Libraries and Frameworks: Libraries like jQuery or frameworks like React and
Angular offer additional validation features.
Example (Simple Form Validation)
JavaScript
function validateForm() {
let name = document.getElementById("name").value;
if (name === "") {
alert("Please enter your name.");
return false;
}
return true;
}
JavaScript Cookies
o When a user sends a request to the server, then each of that request is
treated as a new request sent by the different user.
o So, to recognize the old user, we need to add the cookie with the response
from the server.
o browser at the client-side.
o Now, whenever a user sends a request to the server, the cookie is added
with that request automatically. Due to the cookie, the server recognizes
the users.
1. document.cookie="name=value";
JavaScript Cookie Example
Example 1
Let's see an example to set and get a cookie.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. </head>
5. <body>
6. <input type="button" value="setCookie" onclick="setCookie()">
7. <input type="button" value="getCookie" onclick="getCookie()">
8. <script>
9. function setCookie()
10. {
11. document.cookie="username=Duke Martin";
12. }
13. function getCookie()
14. {
15. if(document.cookie.length!=0)
16. {
17. alert(document.cookie);
18. }
19. else
20. {
21. alert("Cookie not available");
22. }
23. }
24. </script>
25.
26. </body>
27. </html>