In: Computer Science
I already have an alert message that pops up when the number is not filled in correctly. How would I change that alert function to something similar to this function.
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<script>
function phonenumber(inputtxt){
var phoneno = (\(\d{3}\)|\d{3})[-\s]\d{3}-\d{4};
if(inputtxt.value.match(phoneno)){
document.getElementById("yourEntry").innerHTML =
document.getElementById("myphone").value;
}
//if(!inputtxt.value.match(phoneno)){
// alert("Does not Work!")
//}
}
</script>
</head>
<body>
<form name="form1" action="#">
<input type="text" name="myphone" id="myphone" value="" pattern=
"(\(\d{3}\)|\d{3})[-\s]\d{3}-\d{4}" placeholder="(555) 555-1212"
required />
<input type="submit" value="Submit"
onclick="phonenumber(document.form1.myphone)"/>
</form>
Your results are as follows:
<p id="yourEntry">
</p>
</body>
</html>
Here is my complete code that I would like to keep plus having the right message 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(){
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>
So I am going to Edit your second code with Implementation of Alert message:
<!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!==phone){
alert("Please Enter Valid Phone number!!"); // alert Box in case
Phone number format is wrong.
} 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>
This Code should Alert User that Number Format is not right.
Please Consider a Thumps-up(like),It helps alot to answer such questions.