new to this so hopefully my question is specific enough for it to make sense.
I am trying to create a webpage that validates a "Student Number" ---
- must start with an "A"
- following the A there must be exactly 8 numerical digits,
- but they cannot be all 0's
For example A89071231 would be a valid student number, but A00000000 would not be, nor would just "A" or "A123".
The user's input must be A exactly 8 digits that are not all zeros.
What I have so far is this below. Where "StudentNo" = an HTML id tag and the "calculate" function is to total the students grade average.
I just need help figuring out how to write the code so that each digit following the first character A can be anything between 0-9 and still give a valid input, besides a situation where the user inputs all 0's after the "A".
I figured regex would be the easiest way to do it, or by using substr() or substring() but I am not very familiar with JavaScript and can't seem to figure out where I am going wrong with it.
Thanks for any help you can give!!!
function calculate();
var sno = $("StudentNo").value.trim();
if(sno === "" || !sno.startsWith("A") {
alert("Student Number is missing, please enter your Student A Number.");
$("StudentNo").focus();
return;
}
CodePudding user response:
You just have to handle 3 conditioin as
1) !sno.startsWith("A")
2) sno.length !== 9
3) sno.slice(1).match(/0{8}/)
function validateSNo(sno) {
if (!sno.startsWith("A") || sno.length !== 9 || sno.slice(1).match(/0{8}/)) {
// alert("Student Number is missing, please enter your Student A Number.");
// $("StudentNo").focus();
console.log(
"Student Number is missing, please enter your Student A Number."
);
} else {
console.log("Perfect student number")
}
}
validateSNo("S");
validateSNo("A00000000");
validateSNo("A123");
validateSNo("A12345678");
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
I hope following answers will help you
I have checked following thinks
- First character A or not
- Rest of the first character has only number or not (A12345678 is valid, A1234567B invalid)
- Rest of the first character has 8 digits or not (A 8 digits or not)
- Rest of the first character has only 0 value or not (A00000000 or not)
Code Snippet
function checkStudentNumber(studentNumber){
var sno = studentNumber; // get student number
var checkInitialCharacter = sno.charAt(0) === "A" ? true : false; // check studentno first character A or not, if A output will be true
var exeptFirstLetter = sno.slice(1); // Separate execpt first character from string
var checkexeptFirstLetterCharactersLength = exeptFirstLetter.length == 8 ? true : false; // character separated charactee count qual to 8 or not, if YES output will be true.
var checkExceptFirstCharacters = /^\d $/.test(exeptFirstLetter) ? true : false; // confirm rest of the first character has only numbers.
var convertExecptFirstlettersintoNumber = checkExceptFirstCharacters ? parseFloat(exeptFirstLetter) : null; // convert rest of the first character into number
var checkZeroValues = convertExecptFirstlettersintoNumber === 0 ? true : false; // check rest of the first character has only zero digits or not,
if(!checkInitialCharacter || checkZeroValues || !checkexeptFirstLetterCharactersLength || !checkExceptFirstCharacters ){
alert(studentNumber " : Student Number is missing, please enter your Student A Number.");
} else {
alert(studentNumber " : Perfect student number");
}
}
checkStudentNumber("A00000000"); // Output: Student Number is missing, please enter your Student A Number.
checkStudentNumber("B00000000"); // Output: Student Number is missing, please enter your Student A Number.
checkStudentNumber("A12345"); // Output: Student Number is missing, please enter your Student A Number.
checkStudentNumber("A12345678"); // Output: Perfect student number.
checkStudentNumber("ABCD78"); // Output: Student Number is missing, please enter your Student A Number.
checkStudentNumber("A0000000B"); // Output: Student Number is missing, please enter your Student A Number.
checkStudentNumber("A12345B78"); // Output: Student Number is missing, please enter your Student A Number.
checkStudentNumber("A45645678"); // Output: Perfect student number.
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
You should be able to use Regular Expressions with Negative lookahead assertion,
using the group: (?!0{8})
. We'd follow this with the required 8 digits (\d{8})
.
We'd wrap this up in a function checkStudentNumber()
, which takes the student number as the only parameter and returns a boolean true/false value indicating if the number is valid.
function checkStudentNumber(number) {
return /^A(?!0{8})(\d{8})$/.test(number)
}
const numbers = ['A12345670', 'A00000001', 'A00010000', 'A12345678','A00000000', 'A1234', 'A123456789', 'GARBAGE'];
console.log('Tests:');
console.log('\nNumber \tValid');
numbers.forEach(number => console.log(number.padEnd(8, ' '), '\t', checkStudentNumber(number) ? 'Yes': 'No'));
.as-console-wrapper { max-height: 100% !important; top: 0; }
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>