<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
<h1 class="mt-3">Chuẩn hóa chuỗi nhập địa chỉ</h1>
<div class="mt-3">
<label class="form-label">Họ và tên:</label>
<input type="text" class="form-control" id="name">
</div>
<div class="mt-3">
<label class="form-label">Địa chỉ:</label>
<input type="text" class="form-control" id="address" onblur="addressStandardize()">
</div>
<div class="mt-3">
<label class="form-label">Điện thoại:</label>
<input type="text" class="form-control" id="phone">
</div>
</div>
<script src="script.js"></script>
</body>
</html>
// Đoạn chương trình cho phép nhập văn bản bằng giọng nói
const inputs = document.querySelectorAll('input');
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
// Đặt ngôn ngữ là tiếng Việt
recognition.lang = 'vi-VN';
inputs.forEach(input => {
input.addEventListener('click', () => {
recognition.start();
recognition.onresult = (event) => {
const transcript = event.results[0][0].transcript;
input.value = transcript;
};
});
});
function addressStandardize() {
// Loại bỏ khoảng trắng thừa ở đầu và cuối chuỗi
address = $("#address").val();
address = address.trim();
// Thay thế các cụm từ chỉ đường phố, phường, quận, thành phố bằng các từ viết tắt chuẩn
address = address.replace(/đường/g, "đường");
address = address.replace(/phường/g, "phường");
address = address.replace(/quận/g, "quận");
address = address.replace(/thành phố/g, "thành phố");
// Thêm dấu phẩy vào giữa các thành phần của địa chỉ nếu cần
if (!address.includes(",")) {
address = address.replace(/ (đường|phường|quận|thành phố) /g, ", $1 ");
}
// Loại bỏ khoảng trắng thừa
address = address.replace(/s+/g, " ");
// Xuất giá trị sau khi đã chuẩn hóa
$("#address").val(address);
}