Skip to content

Commit a598c86

Browse files
authored
Passport information validation (#1511)
* passportvalidity.js * readme.md
1 parent 2eef425 commit a598c86

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function onChange(control, oldValue, newValue, isLoading) {
2+
if (isLoading) return;
3+
4+
var passportNumber = g_form.getValue('passport_number');
5+
var dateOfIssue = g_form.getValue('date_of_issue');
6+
var age = parseInt(g_form.getValue('age'), 10);
7+
var dateOfExpiry = g_form.getValue('date_of_expiry');
8+
9+
// Passport Number Validation
10+
var passportPattern = /^[A-Z][1-9][0-9][A-Z0-9]{5}$/;
11+
if (passportNumber && !passportPattern.test(passportNumber)) {
12+
g_form.showFieldMsg('passport_number', "The entered number is invalid passport number format. It must be 8 characters long, start with an uppercase letter, followed by a number between 1-9, then 0-9, and the rest alphanumeric.", "error");
13+
g_form.clearValue('passport_number');
14+
} else {
15+
g_form.hideFieldMsg('passport_number');
16+
}
17+
18+
// Date of Expiry Calculation based on date of issue
19+
if (dateOfIssue && age) {
20+
var issueDate = new GlideDate();
21+
issueDate.setValue(dateOfIssue);
22+
var expiryDate = new GlideDate();
23+
expiryDate.setValue(issueDate);
24+
25+
if (age >= 18) {
26+
expiryDate.addYears(5); // Adult - add 5 years
27+
} else {
28+
expiryDate.addYears(10); // Under 18 - add 10 years
29+
}
30+
31+
g_form.setValue('date_of_expiry', expiryDate.getByFormat('yyyy-MM-dd')); // Set expiry date in correct format
32+
g_form.hideFieldMsg('date_of_expiry');
33+
} else if (!dateOfIssue) {
34+
g_form.showFieldMsg('date_of_issue', "Please enter the Date of Issue first.", "info");
35+
} else if (!age) {
36+
g_form.showFieldMsg('age', "Please enter your age first.", "info");
37+
}
38+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
This OnChange Catalog Client Script is for validating passport number, date of issue, and date of expiry.
2+
3+
It follows the specified rules(As per indian passport):-
4+
- The passport number should be 8 characters long, with the first character as an uppercase letter, the second and third characters as numbers (1-9 for the first digit, 0-9 for the second digit).
5+
- The date of expiry should be calculated based on the date of issue:
6+
- 1. If the user is an adult (18 years or older), the expiry date should be exactly 5 years from the date of issue.
7+
2. If the user is under 18, the expiry date should be exactly 10 years from the date of issue.
8+
9+
Passport Number Validation:
10+
The passportPattern uses a regular expression:
11+
^[A-Z] – The first character must be an uppercase letter.
12+
[1-9][0-9] – The second and third characters are numbers; the first is between 1-9, and the second between 0-9.
13+
[A-Z0-9]{5}$ – The last five characters are alphanumeric.
14+
If the passport number does not match this pattern, the script displays an error message and clears the field.
15+
16+
Date of Expiry Calculation:
17+
- After the date of issue and age are provided, the script calculates the expiry date by adding 5 years for adults (18 or older) or 10 years for minors (under 18).
18+
- The calculated expiry date is automatically set in the date_of_expiry field in the yyyy-MM-dd format.
19+
- Prompts are displayed if necessary fields like date_of_issue or age are missing before attempting the expiry date calculation.
20+
21+
This Client Script will ensure that the entered passport information and expiry date meet the requirements, providing a seamless and guided experience for the user.

0 commit comments

Comments
 (0)