In: Computer Science
Here is what I have so far. I have created a code where a user can enter in their information and when they click submit all of the information is shown. How can I add a required field for the phone number without using an alert?
<!Doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Login and Registeration Form
Design</title>
<link rel="stylesheet" type="text/css" href="signin.css">
<script>
function myFunction(){
document.getElementById('demo').innerHTML =
document.getElementById('fname').value + " " +
document.getElementById('lname').value + " " +
document.getElementById('street').value + " " +
document.getElementById('city').value + " " +
document.getElementById('zcode').value + " " +
document.getElementById('email').value + " " +
document.getElementById('phone').value;
}
</script>
</head>
<body>
<div class="login-page">
<div class="form">
<br>
<h1> Register </h1>
<input type="text" id="fname" placeholder="first
name"/>
<input type="text" id="lname" placeholder="last name"/>
<input type="text" id="street" placeholder="street
address"/>
<input type="text" id="city" placeholder="city"/>
<input type="text" id="zcode" placeholder="zip code"/>
<input type="text" id="email" placeholder="email
address"/>
<input type="text" id="phone" value=""
pattern="(\(\d{3}\)|\d{3})[-\s]\d{3}-\d{4}"
placeholder="XXX-XXX-XXXX" required/>
<button onclick="myFunction()">Create</button>
<label for="create login" id="create
login"></label>
<p id="demo"></p>
</div>
</div>
</body>
</html>
<!Doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Login and Registeration Form
Design</title>
<link rel="stylesheet" type="text/css" href="signin.css">
<script>
function myFunction(){
phoneno = document.getElementById('phone').value;
console.log("qer"+phoneno);
var error = document.getElementById("error")
if(phoneno===""){
// Changing content and color of content
error.textContent = "Please enter a valid number"
error.style.color = "red"
} else {
error.textContent = ""
document.getElementById('demo').innerHTML =
document.getElementById('fname').value + " " +
document.getElementById('lname').value + " " +
document.getElementById('street').value + " " +
document.getElementById('city').value + " " +
document.getElementById('zcode').value + " " +
document.getElementById('email').value + " " +
document.getElementById('phone').value;
}
}
</script>
</head>
<body>
<div class="login-page">
<div class="form">
<br>
<h1> Register </h1>
<input type="text" id="fname" placeholder="first
name"/>
<input type="text" id="lname" placeholder="last name"/>
<input type="text" id="street" placeholder="street
address"/>
<input type="text" id="city" placeholder="city"/>
<input type="text" id="zcode" placeholder="zip code"/>
<input type="text" id="email" placeholder="email
address"/>
<input type="text" id="phone" value=""
pattern="(\(\d{3}\)|\d{3})[-\s]\d{3}-\d{4}"
placeholder="XXX-XXX-XXXX" required/>
<span id="error"></span>
<button onclick="myFunction()">Create</button>
<label for="create login" id="create
login"></label>
<p id="demo"></p>
</div>
</div>
</body>
</html>
Note: If you want to use "required" attribute, use form tag and submit button. To create form and submit it. Otherwise it won't work. Here, I have used javascript to check if the value is null or not and show error message accordingly.