The first character cannot be a space, dot, or dash in input form using jacescript.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Input Filter</title>
</head>
<body>
<input type="text" id="myInput" placeholder="Enter text">
<script>document.getElementById('myInput').addEventListener('keyup', function (e) {
let value = e.target.value;
let filteredValue = value.replace(/[^a-zA-Z0-9 .-]/g, '');
// Ensure the first character is not a space, dot, or dash
if (/^[ .-]/.test(filteredValue)) {
filteredValue = filteredValue.slice(1);
alert('The first character cannot be a space, dot, or dash.');
}
// If a disallowed character was found, show an alert
if (value !== filteredValue) {
alert('Special characters other than spaces, dots, and dashes are not allowed.');
}
e.target.value = filteredValue;
});
</script>
</body>
</html>
No comments:
Post a Comment