Question

SPEC2000 PN validation

  • 22 April 2024
  • 0 replies
  • 7 views

Userlevel 3
Badge +8

How can I implement some validation of content in Part Number field to ensure validity with ATA Spec2000 standard?

I have the minimum rules in the following JS code. Is there a preferred way for data validation at input, before SAVE (which could be ran through an event) .. 

            //function to verify part number meets Spec 2000
function testPN() {
let inputText = document.getElementById("pnText").value;
//maximum number of characters allowed in the part number
let maxChars = 15;

//remove any leading and/or trailing spaces from the inputted part number
inputText = inputText.trim();

//Alert user if the submit button was pressed with no part number entered
if (inputText == "") {
alert("Please enter a part number to analyze.");
} else {
//define variables
let errorFound = false;
let errorText = [];

//Verify max length
if (inputText.length > maxChars) {
errorFound = true;
errorText.push("The maximum number of characters is " + maxChars + ". The part number entered has " + inputText.length + ".");
}

//Verify no spaces
if (inputText.indexOf(' ') != -1) {
errorFound = true;
errorText.push("No spaces are allowed in the part number per ATA Spec 2000.");
}

//Verify uppercase only
let lowerPatt = new RegExp("[a-z]");
if (lowerPatt.test(inputText)) {
errorFound = true;
errorText.push("No lowercase letters are allowed per ATA Spec 2000.");
}

//Verify no use of the letter "O"
if (inputText.indexOf('O') != -1 || inputText.indexOf('o') != -1) {
errorFound = true;
errorText.push("The letter \"O\" is not allowed per ATA Spec 2000.");
}

//Verify does not start or end with "-"
let startingDash = new RegExp("^-");
let endingDash = new RegExp("-$");

if (startingDash.test(inputText) || endingDash.test(inputText)) {
errorFound = true;
errorText.push("The part number cannot start or end with a \"-\" per ATA Spec 2000.");
}

//Verify only use "-" beteen numeric characters
let numHyphen = new RegExp("[^0-9]-");
let hyphenNum = new RegExp("-[^0-9]");

if (numHyphen.test(inputText) || hyphenNum.test(inputText)) {
errorFound = true;
errorText.push("Only numeric characters are allowed next to a hyphen per ATA Spec 2000.");
}

//verify no special characters used
//NOTE: lowercase letters, periods, and spaces are included since they are being checked elsewhere.
// This prevents duplicate feedback entries.
let nonSpecial = new RegExp("[^0-9a-zA-Z-. ]");

if (nonSpecial.test(inputText)) {
errorFound = true;
errorText.push("No special characters (e.g. /, \\, *, &, #, etc.) are allowed per ATA Spec 2000.");
}


//Look for periods and warn if used
if (inputText.indexOf('.') !== -1) {
errorText.push("Note: The use of periods in the part number is allowed, but not recommended.");
}

//Compile feedback to user
let errorMessage = "";

if (errorFound) {
errorMessage = "<p style='color: red;'>The part number is not compliant with ATA Spec 2000.</p><p>";
for (let i = 0; i < errorText.length; i++) {
errorMessage += (i + 1) + ". " + errorText[i] + "<br>";
}
errorMessage += "</p>";

} else {
errorMessage = "<p>The part number is compliant with ATA Spec 2000.</p><p>";
for (let i = 0; i < errorText.length; i++) {
errorMessage += (i + 1) + ". " + errorText[i] + "<br>";
}
errorMessage += "</p>";
}

document.getElementById("feedback").innerHTML = errorMessage;

}
}

 


0 replies

Be the first to reply!

Reply