[go: up one dir, main page]

0% found this document useful (0 votes)
193 views4 pages

Password, Email & URL Validation

This document discusses validating email addresses and URLs using regular expressions in JavaScript. It provides examples of parsing URLs to extract components like the protocol and hostname. It also provides an example of validating an email address without regular expressions by checking for characters like @ and . and their positions. Finally, it shares code to validate an email address entered in a form by checking for these special characters and lengths using string methods instead of a regex.

Uploaded by

Sam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
193 views4 pages

Password, Email & URL Validation

This document discusses validating email addresses and URLs using regular expressions in JavaScript. It provides examples of parsing URLs to extract components like the protocol and hostname. It also provides an example of validating an email address without regular expressions by checking for characters like @ and . and their positions. Finally, it shares code to validate an email address entered in a form by checking for these special characters and lengths using string methods instead of a regex.

Uploaded by

Sam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Password, Email, URL validation using RegEx

1. Parsing and processing URL using RegEx:


URL or Uniform Resource Locator consists of many information parts, such as the domain name, path, port
number etc. Any URL can be processed and parsed using Regular Expression.

Example:
URL: https://www.mit.ac.in
When we parse the above URL then we can find

Hostname: mitindia.edu
Protocol: https

re.findall( ) function of re library for searching the required pattern in the URL.
Syntax: re.findall(regex, string)
Return: all non-overlapping matches of pattern in string, as a list of strings.

Example 1: In this Example, we will be extracting the protocol and the hostname from the given URL.

• Regular expression for extracting protocol group: ‘(\w+)://‘.


• Regular expression for extracting hostname group: ‘://www.([\w\-\.]+)‘.

Meta characters Used:

• \w: Matches any alphanumeric character, this is equivalent to the class [a-zA-Z0-9_].
• +: One or more occurrences of previous characters.
Code:

# import library
import re
# url link
s = 'https://www.mitindia.edu/'

# finding the protocol


obj1 = re.findall('(\w+)://',s)
print(obj1)

# finding the hostname which may


# contain dash or dots
obj2 = re.findall('://www.([\w\-\.]+)',s)
print(obj2)

Output:
['https']
['mitindia.edu']
Example 2: If the URL is of a different type such as ‘file://localhost:4040/zip_file‘, with the port number
along with it, then to extract the port number, as it is optional we will use the ‘?’ notation. Here the port
number ‘4040′ occurs after the ‘:’ sign. Therefore, as it is a digit (:(\d+)) is used. To make it optional as
all URLs do not end with host number, this syntax is used ‘(:(\d+))?’.
Meta characters Used:
• \d: Matches any decimal digit, this is equivalent to the set class [0-9].
• +: One or more occurrences of previous characters.
• ?: Matches zero or one occurrence.

Code:
# import library
import re
# url link
s = 'file://localhost:4040/abc_file'

# finding the file capture group


obj1 = re.findall('(\w+)://', s)
print(obj1)

# finding the hostname which may


# contain dash or dots
obj2 = re.findall('://([\w\-\.]+)', s)
print(obj2)

# finding the hostname which may


# contain dash or dots or port number
obj3 = re.findall('://([\w\-\.]+)(:(\d+))?', s)
print(obj3)

Output:
['file']
['localhost']
[('localhost', ':4040', '4040')]

Example 3: For a general URL, this can be used, where the path elements can also be constructed.

# import library
import re
# url
s = 'http://www.example.com/index.html'
# searching for all capture groups
obj = re.findall('(\w+)://([\w\-\.]+)/(\w+).(\w+)', s)
print(obj)

Output:
[('http', 'www.example.com', 'index', 'html')]
2. How to Validate Email Address without using Regular 0Expression in JavaScript?

• Email validation in JavaScript is the process of ensuring that an email address entered by the user is
in the correct format and is a valid email address or not.
• This is typically done on the client side using JavaScript before the form is submitted to the server.
• There are several methods that can be used to perform email validation in JavaScript, including using
string methods, regular expressions, and libraries.
• These methods can check if certain characters, such as the “@” symbol and the “.” character, are
present in the email address in the correct positions and if the email address contains valid characters,
such as alphabets, numbers, periods, underscores, and hyphens.
• However, these methods do not check if the domain name is valid or exists.

Validate email address by using String methods:


1. Use the indexOf() method to check if the email address contains an “@” symbol. The indexOf()
method returns the index of the first occurrence of the specified string value, or -1 if the value is not
found. If the return value is -1, it means that the email address does not contain an “@” symbol, and
if the return value is 0, it means that the “@” symbol is present at the first position, so the validation
fails.
2. Use the lastIndexOf() method to check if the email address contains a “.” symbol. The lastIndexOf()
method returns the index of the last occurrence of the specified string value, or -1 if the value is not
found. If the return value is -1, it means that the email address does not contain a “.” symbol, and if
the return value is 0, it means that the “.” symbol is present at the first position, so the validation
fails.
3. Check if the “.” symbol is after the “@” symbol by comparing the index of the “@” and “.” symbols,
if not validation fails.
4. Check that there must be something after the “.” symbol otherwise, the validation fails.
5. Finally, check if there is no space in the email address by using the indexOf() method to check if the
email address contains a ” ” space. If the return value is not -1, it means that the email address
contains a space, and the validation fails.
CODE:
<!DOCTYPE html>
<html>

<head>
<title>Validate Email Address</title>

<script type="text/javascript">

// JavaScript code for email validation


// without using regular expression
function validateEmailAddress(emailAddress) {
var atSymbol = emailAddress.indexOf("@");
var dotSymbol = emailAddress.lastIndexOf(".");
var spaceSymbol = emailAddress.indexOf(" ");

if ((atSymbol != -1) &&


(atSymbol != 0) &&
(dotSymbol != -1) &&
(dotSymbol != 0) &&
(dotSymbol > atSymbol + 1) &&
(emailAddress.length > dotSymbol + 1) &&
(spaceSymbol == -1)) {
alert("Email address is valid.");
return true;
} else {
alert("Error !!! Email address is not valid.");
return false;
}
}
</script>
</head>

<body>
<h1>Please enter your email address below</h1>
<form name="the form" action="" method="post"
onSubmit="var the_result = validateEmailAddress(this.email_address.value); return the_result;">
Email address: <input type="text" name="email_address">
<input type="submit" value="Submit form">
</form>
</body>

</html>

Output:
Please enter your email address below:
Email address: arunkumar.a@vit.ac.in
Submit form.

You might also like