Fill the validateForm function to check that the phone number contains a number
ID: 3713470 • Letter: F
Question
Fill the validateForm function to check that the phone number contains a number (use the isNaN function) and that the user name is less than 11 characters long. Display "Phone number is invalid" and/or "User name is invalid" in the console log if the check does not pass. Use the preventDefault function to avoid submitting the form when the inputs are invalid. NEEDS TO BE IN JAVASCRIPT!
html code:
<form id="userForm" action="https://learn.zybooks.com" method="POST">
<label for="userName">Username:</label>
<input type="text" id="userName"><br>
<input type="text" id="phoneNumber"><br>
<input type="submit" id="submitForm">
</form>
JAVA:
var form = document.getElementById("userForm");
function validateForm(event) {
var phoneNumber = form.phoneNumber.value;
var userName = form.userName.value;
/* Your solution goes here */
}
form.addEventListener("submit", validateForm);
Explanation / Answer
//html javascript code:
<html>
<head>
<body>
<form id="userForm" action="https://learn.zybooks.com" method="POST">
<label for="userName">Username:</label>
<input type="text" id="userName"><br>
<input type="text" id="phoneNumber"><br>
<input type="submit" id="submitForm">
</form>
</body>
<script type="text/javascript">
var form = document.getElementById("userForm");
function validateForm(event) // function name to validate the form
{
var phoneNumber = form.phoneNumber.value;
var userName = form.userName.value;
/* Your solution goes here */
if (!isNaN(phoneNumber))/*this if statement returns true if the eenterd numbe is numbers ,return false if the entered not a number*/
{
alert("Phone number is invalid" );
return false;
}
else
return true;
if(username.length<11)/*here username is less than 11 character it will return false otherwise true*/
{
alert("User name is invalid");
return false;
}
}
form.addEventListener("submit", validateForm);
</script>
</head>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.