re表示name
const re= /^[a-zA-Z]{2,10}$/;
re表示zip
const re= /^[0-9]{5}(-[0-9]{4})?$/;
re表示email
const re= /^([a-zA-Z0-9_\?\.]+)@([a-zA-Z0-9_\?\.]+)\.([a-zA-Z]{2,5})$/;
re表示phone
const re= /^\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4}$/;
app.js
document.getElementById('name').addEventListener('blur',validateName);
document.getElementById('zip').addEventListener('blur',validateZip);
document.getElementById('email').addEventListener('blur',validateEmail);
document.getElementById('phone').addEventListener('blur',validatePhone);
function validateName(){
const name= document.getElementById('name');
const re= /^[a-zA-Z]{2,10}$/;
if(!re.test(name.value)){
name.classList.add('is-invalid');
}else{
name.classList.remove('is-invalid');
}
}
function validateZip(){
const zip= document.getElementById('zip');
const re= /^[0-9]{5}(-[0-9]{4})?$/;
if(!re.test(zip.value)){
zip.classList.add('is-invalid');
}else{
zip.classList.remove('is-invalid');
}
}
function validateEmail(){
const email= document.getElementById('email');
const re= /^([a-zA-Z0-9_\?\.]+)@([a-zA-Z0-9_\?\.]+)\.([a-zA-Z]{2,5})$/;
if(!re.test(email.value)){
email.classList.add('is-invalid');
}else{
email.classList.remove('is-invalid');
}
}
function validatePhone(){
const phone= document.getElementById('phone');
const re= /^\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4}$/;
if(!re.test(phone.value)){
phone.classList.add('is-invalid');
}else{
phone.classList.remove('is-invalid');
}
}
index.html
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 mx-auto">
<h1>User Form</h1>
<form>
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" id="name" placeholder="Name">
<div class="invalid-feedback">
Name must be between 2 and 10 characters
</div>
</div>
<div class="form-group">
<label>Zipcode</label>
<input type="text" class="form-control" id="zip" placeholder="Zipcode">
<div class="invalid-feedback">
Enter a valid Zipcode
</div>
</div>
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" id="email" placeholder="Email">
<div class="invalid-feedback">
Enter a valid Email
</div>
</div>
<div class="form-group">
<label>Phone Number</label>
<input type="text" class="form-control" id="phone" placeholder="Phone Number">
<div class="invalid-feedback">
Enter a valid Phone Numebr
</div>
</div>
</div>
<input type="submit" value="submit" class="btn btn-primary btn-block">
</form>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
<script src="app.js"></script>
</body>
</html>