Uploaded by switzgitonga.sg

js lab3

advertisement
function validateForm() {
// Get input values
var firstName = document.forms["registrationForm"]["firstName"].value;
var secondName = document.forms["registrationForm"]["secondName"].value;
var gender = document.forms["registrationForm"]["gender"].value;
var yearOfBirth = document.forms["registrationForm"]["yearOfBirth"].value;
var kcseGrade = document.forms["registrationForm"]["kcseGrade"].value;
// Regular expression patterns for validation
var namePattern = /^[a-zA-Z]+$/;
var numberPattern = /^[0-9]+$/;
// Check if any field is left empty
if (
firstName === "" ||
secondName === "" ||
gender === "" ||
yearOfBirth === "" ||
kcseGrade === ""
) {
alert("Please fill in all fields.");
return false;
}
// Check if fields contain only alphabets or numbers
if (!firstName.match(namePattern)) {
alert("First name must contain only alphabets.");
return false;
}
if (!secondName.match(namePattern)) {
alert("Second name must contain only alphabets.");
return false;
}
if (!yearOfBirth.match(numberPattern)) {
alert("Year of birth must contain only numbers.");
return false;
}
// Calculate age based on year of birth
var age = new Date().getFullYear() - yearOfBirth;
// Check if age is greater than or equal to 17
if (age < 17) {
alert("You must be at least 17 years old to register.");
return false;
}
// Check if KCSE grade is greater than or equal to C+
const validGrades=['A+','A-','A','B+','B','B-','C+']
if(!validGrades.includes(kcseGrade)){
alert("You need a minimum KCSE grade of C+ to register.");
return false;
}
return true;
}
function checkEligibility() {
if (validateForm()) {
alert("Congratulations! You are eligible for university admission.");
} else {
alert("Sorry, you do not meet the eligibility requirements for
university admission.");
}
return false;
}
Download