In: Computer Science
Is there a way to have the same response when someone does not fill out the number correctly on my second code. Is there a way to not use the alert.
<!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 the complete code that I have so far. My goal is to use the same alert as the first code shown.
<!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() {
//checking phone number
if(phonenumber(document.getElementById('phone').value))
{
//when phone number is valid
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;
}
}
//function to validate phone number
function phonenumber(inputtxt) {
//regular expression for phone number
var phoneno = /^(\(\d{3}\)|\d{3})[-\s]\d{3}-\d{4}/;
//checking phone number
if (inputtxt.match(phoneno)) {
//when phone number is valid
return true;
}
else{
alert("Phone number is invalid!");//alert to the user
return false;
}
}
</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>
Yes, there is a way - you have to use an HTML5 form. In the first code snippet, an HTML5 form is used. The form validation is taking place just because of that. It has nothing to do with the onclick and the code written in the phonenumber function.
If you check the log when you load the first code snippet in the browser, you will find the error logs in the code. Here is a documented correct way of triggering the validation bubble in the first code snippet:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Document Title</title>
</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 />
<!-- I have changed the type of the button below and introduced a hidden button which is there to actually submit the form -->
<input type="button" value="Submit" onclick="phonenumber()"/>
<input id="submitButton" type="submit" value="Submit" hidden>
</form>
Your results are as follows:
<p id="yourEntry"></p>
<script>
function phonenumber()
{
// this function is triggerred by clicking the Submit button
if(document.getElementsByName('form1')[0].checkValidity())
{
// if the validation fails, the bubble shows the error message
// otherwise, the hidden button with id: submitButton is clicked and the form submission takes place
document.getElementById('submitButton').click();
}
}
</script>
</body>
</html>
Similarly, you can introduce a form in the second code snippet you wrote and add a hidden button for submission. Remember to use the other button just to call the function, no submission using that. The function will click the hidden button if the HTML5 validation succeeds, otherwise it will show the validation bubble.
Hope this helps! :)