DS27
Q. 1) Create web Application that contains Voters details and check proper validation for (name,
age, and nationality), as Name should be in upper case letters only, Age should not be less than
18 yrs and Nationality should be Indian.(use HTML-AJAX-PHP) [Marks 15]
$.ajax({
url: 'validate_voter.php',
method: 'POST',
data: {name: name, age: age, nationality: nationality},
success: function(response) {
$("#result").html(response);
}
});
});
});
</script>
$errors = array();
// Validate name (Upper Case Letters Only)
if (!preg_match('/^[A-Z\s]+name)) {
$errors[] = "Name should contain upper case letters only.";
}
// Validate age (>= 18)
if (age < 18) {
errors[] = "Age should be 18 years or older.";
}
// Validate nationality (Indian Only)
if (strtolower(nationality) !== 'indian') {
errors[] = "Nationality should be Indian.";
}
// Display validation result
if (empty(errors)) {
echo "<p style='color: green;'>Validation successful. Voter details are valid.</p>";
} else {
echo "<p style='color: red;'>Validation failed:</p>";
echo "<ul>";
foreach (errors as error) {
echo "<li>error</li>";
}
echo "</ul>";
}
?>
Q. 2 ) Create your own transactions dataset and apply the above process on your dataset
transactions = [
{"Transaction ID": 1, "Amount": 50.00, "Date": "2024-04-01", "Location": "New York"},
{"Transaction ID": 2, "Amount": 30.50, "Date": "2024-04-02", "Location": "Los Angeles"},
{"Transaction ID": 3, "Amount": 100.00, "Date": "2024-04-03", "Location": "Chicago"},
{"Transaction ID": 4, "Amount": 25.75, "Date": "2024-04-04", "Location": "Miami"},
{"Transaction ID": 5, "Amount": 70.20, "Date": "2024-04-05", "Location": "San Francisco"}
]
def validate_transactions(transactions):
errors = []
for transaction in transactions:
transaction_id = transaction["Transaction ID"]
amount = transaction["Amount"]
date = transaction["Date"]
location = transaction["Location"]
# Example validation rules
if amount <= 0:
errors.append(f"Transaction ID {transaction_id}: Amount should be greater than zero.")
# Add more validation rules as needed...
# Display validation result
if not errors:
print("Validation successful. Transactions dataset is valid.")
else:
print("Validation failed:")
for error in errors:
print(error)