Validate

The Validate class is a specialized utility that ensures the correctness of national ID numbers by conducting multiple validation checks. It assesses whether the ID has the appropriate length, cont ains valid characters, includes the correct "V" or "X" designation for first-generation IDs, and v erifies the accuracy of the encoded date of birth. These validations ensure that the ID conforms t o the expected format and standards. Each check is performed through dedicated methods, such as is ValidLength for checking length, isValidCharacters for ensuring only allowed characters are used, and isValidDayRange for verifying the date of birth. A core function of the class is the isValidNIC method, which aggregates all the validation checks and returns a boolean indicating whether the ID is valid. If the ID passes all tests, the method returns true; otherwise, it returns false. For a more granular analysis, the invalidsCount method provides th e number of failed checks, giving insight into where the errors occur in the ID. This functionality a llows the class to act as a comprehensive validator, ensuring that each part of the ID is thoroughly examined before determining its validity. The Validate class also offers individual methods for focused validation. For example, isValidLength checks whether the ID conforms to the expected length, while isValidCharacters determines if only va lid characters are used. The isValidVXInOldGenAndNotInNewGen method checks if first-generation IDs c ontain the "V" or "X" characters, which are required for those IDs but not for second-generation IDs. This flexibility allows developers to validate specific aspects of the ID when needed, making the cla ss both versatile and detailed. An important aspect of the Validate class is its handling of exceptions. It extends from an exception class, which allows it to manage cases where invalid parameters are passed, such as when the ID numbe r is undefined or not in the correct string format. The #exceptionTest private method checks for thes e scenarios, ensuring that invalid input is caught early, preventing further errors. This robust erro r-handling mechanism makes the class reliable in diverse use cases where data may not always be in th e expected form. The class constructor includes an optional exceptionSwitch parameter, which gives users control over whether exceptions are enabled or disabled. By default, exceptions are enabled, but developers can s et this to false if they prefer to handle errors differently. This flexibility ensures that the Vali date class can be used in various contexts, providing strong validation capabilities and control over how exceptions are managed. Overall, the class is designed to be a complete and user-friendly solutio n for National ID validation.

Description:
  • The Validate class is a specialized utility that ensures the correctness of national ID numbers by conducting multiple validation checks. It assesses whether the ID has the appropriate length, cont ains valid characters, includes the correct "V" or "X" designation for first-generation IDs, and v erifies the accuracy of the encoded date of birth. These validations ensure that the ID conforms t o the expected format and standards. Each check is performed through dedicated methods, such as is ValidLength for checking length, isValidCharacters for ensuring only allowed characters are used, and isValidDayRange for verifying the date of birth. A core function of the class is the isValidNIC method, which aggregates all the validation checks and returns a boolean indicating whether the ID is valid. If the ID passes all tests, the method returns true; otherwise, it returns false. For a more granular analysis, the invalidsCount method provides th e number of failed checks, giving insight into where the errors occur in the ID. This functionality a llows the class to act as a comprehensive validator, ensuring that each part of the ID is thoroughly examined before determining its validity. The Validate class also offers individual methods for focused validation. For example, isValidLength checks whether the ID conforms to the expected length, while isValidCharacters determines if only va lid characters are used. The isValidVXInOldGenAndNotInNewGen method checks if first-generation IDs c ontain the "V" or "X" characters, which are required for those IDs but not for second-generation IDs. This flexibility allows developers to validate specific aspects of the ID when needed, making the cla ss both versatile and detailed. An important aspect of the Validate class is its handling of exceptions. It extends from an exception class, which allows it to manage cases where invalid parameters are passed, such as when the ID numbe r is undefined or not in the correct string format. The #exceptionTest private method checks for thes e scenarios, ensuring that invalid input is caught early, preventing further errors. This robust erro r-handling mechanism makes the class reliable in diverse use cases where data may not always be in th e expected form. The class constructor includes an optional exceptionSwitch parameter, which gives users control over whether exceptions are enabled or disabled. By default, exceptions are enabled, but developers can s et this to false if they prefer to handle errors differently. This flexibility ensures that the Vali date class can be used in various contexts, providing strong validation capabilities and control over how exceptions are managed. Overall, the class is designed to be a complete and user-friendly solutio n for National ID validation.

Source:
Author:
  • Charitha Prabhashwara

Classes

Validate

Methods

(inner) invalidsCount(nicNumber) → {int}

Description:
  • Performs all validation checks and outputs how many validation checks have failed.

Source:
Example
let count= Validate().invalidsCount("your national-identity-card number");
console.log(count)
Parameters:
Name Type Description
nicNumber string
Returns:
Type
int

(inner) isInvalidNIC(nicNumber) → {boolean}

Description:
  • This method checks whether the provided National ID number is invalid by performing a series of validation tests. It returns true if one or more of the validation checks fail, indicating that the ID is invalid. If all validation tests pass, the method returns false, meaning the ID is valid. The validation includes checks for length, valid characters, presence of "V/X" in first-generation IDs, and the encoded date of birth. The National ID number must be provided as a string, and any other data type will trigger an exception.

Source:
Example
let nic = new Validate().isInvalidNIC("your national-identity-card number");
if (nic) {
    console.log("Invalid");
} else {
    console.log("Valid");
}
Parameters:
Name Type Description
nicNumber string

The National ID number to be validated.

Throws:
  • NIC number cannot be undefined and must be of type string.

    Type
    TypeError
  • National ID number must be of type 'string' and cannot be undefined.

    Type
    TypeError
Returns:
  • Returns true if the ID is invalid, and false if it is valid.
Type
boolean

(inner) isValidCharacters(nicNumber) → {boolean}

Description:
  • The characters of the ID number entered by the user are valid or not (boolean). Returns true if the characters in the ID number are valid, returns false if they are invalid.

Source:
Example
let nic= Validate().isValidCharacters("your national-identity-card number");
if(nic){
     console.log("valid");
}else{
     console.log("invalid");
}
Parameters:
Name Type Description
nicNumber string
Returns:
Type
boolean

(inner) isValidLength(nicNumber) → {boolean}

Description:
  • Length of the entered ID number is valid or not (boolean). Returns true if the length is valid, false if the length is invalid.

Source:
Example
let nic= Validate().isValidLength("your national-identity-card number");
if(nic){
     console.log("valid length");
}else{
     console.log("invalid length");
}
Parameters:
Name Type Description
nicNumber string
Returns:
Type
boolean

(inner) isValidNIC(nicNumber) → {boolean}

Description:
  • This method performs a comprehensive validation of the provided National ID number by checking its length, character validity, the presence of "V/X" for first-genera tion IDs, and the correctness of the encoded date of birth. If all checks pass, th e method returns true, indicating the ID is valid. If any check fails, it returns false

Source:
Example
let nic= new Validate().isValidNIC("your national-identity-card number");
if(nic){
     console.log("valid");
}else{
     console.log("invalid");
}
Parameters:
Name Type Description
nicNumber string

The National ID number to be checked.

Throws:
  • NIC number cannot be undefined and must be of type string.

    Type
    TypeError
  • National ID number must be of type 'string' and cannot be undefined.

    Type
    TypeError
Returns:

(boolean): true if the date of birth is correctly encoded, false if there is an error.

Type
boolean

(inner) isValidVXInOldGenAndNotInNewGen(nicNumber) → {boolean}

Description:
  • First-generation ID numbers include the "V" or "X" character, otherwise, it is treated as an invalid ID number, and false is returned otherwise true is returned. This does not apply to second-generation ID numbers but can be used if you enter a second-generation ID number, it will return true.

Source:
Example
let nic= Validate().isValidVXInOldGenAndNotInNewGen("your national-identity-card number");
if(nic){
     console.log("valid");
}else{
     console.log("invalid");
}
Parameters:
Name Type Description
nicNumber string
Returns:
Type
boolean

(inner) isValidVXInOldGenAndNotInNewGen(nicNumber) → {boolean}

Description:
  • Returns false if the encoded date of birth is incorrect, returns true if the date of birth is correct.

Source:
Example
let nic= Validate().isValidDayRange("your national-identity-card number");
if(nic){
     console.log("valid");
}else{
     console.log("invalid");
}
Parameters:
Name Type Description
nicNumber string
Returns:
Type
boolean