Question about JS form validation:
I want to check these two input (name, question) with one function validateText. But how can I pass the input to the function, I did this but not working.
javascript part:
question = document.getElementById("question");
question.addEventListener("input", validateText);
pname = document.getElementById("name");
pname.addEventListener("input", validateName);
validateText function:
function validateText(e) {
var message = "";
var elem_name = e.name;
if (e.value.length == 0) { message = "You must enter a" + elem_name + "!";}
else if (e.validity.tooShort) { message = "Your" + elem_name + "is too short!";}
e.setCustomValidity(message);
e.title = message;
return message;
}
html part:
<label for="name">Name<span>*</span></label>
<input id="name" name="name" type="text" minlength="2">
<label class="blockLabel" for="question">Ask Us Any Quesion<span>*</span></label>
<textarea id="quesiton" name="question" rows="5" cols="55" minlength="15"></textarea>
When I do function for each instead of a generic validateText function, it can work:
So I think the part I did not get is how to create a generic function and pass value to it.
function validateName(e) {
var message = "";
if (pname.value.length == 0) { message = "You must enter a!";}
else if (pname.validity.tooShort) { message = "Youris too short!";}
pname.setCustomValidity(message);
pname.title = message;
return message;
}
Thank you so much!
Your addeventlistener is problematic, you can directly attach the function for some event in the form element.
Below is the working source code for js validation for two different form elements with generic function.
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script>
function validateText(e) {
var message = "";
var targetElement = e.target || e.srcElement;
if (targetElement.value.length == 0) {
message = "You must enter a value !";
}else if (targetElement.validity.tooShort) {
message = "Your data is too short!";
}else{
message = "Your data is verified";
}
targetElement.setCustomValidity(message);
targetElement.title = message;
alert(message);
return message;
}
</script>
</head>
<body><form>
<label for="name">Name<span>*</span></label>
<input id="name" name="name" type="text" minlength="2" onblur="return validateText(event)">
<label class="blockLabel" for="question">Ask Us Any Quesion<span>*</span></label>
<textarea id="question" name="question" rows="5" cols="55" minlength="15" onblur="return validateText(event)"></textarea>
</form>
</body>
</html>
Question about JS form validation: I want to check these two input (name, question) with one...
NEED HELP with HTML with Javascript embedding
for form validation project below. I have my code
below but I'm stuck with validation. If anyone can fix it, I'd
really appreciate.
******************************************************************************
CODE:
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project
Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>Nice</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<script>
var textFromTextArea;
function getWords(){
var text =...
Add JavaScript code in the “find_primeV2.js” to allow users to enter a number, and then based on the number of user enters, to find out how many prime numbers there are up to and including the user inputted number and then display them on the web page. The following are the detailed steps to complete this assignment: Step 1. [30 points] In “find_primeV2.js”, complete isPrime() function by (1) Adding one parameter in function header. That parameter is used to accept...
I need to complete 3 validation cases in this Javascript code. I need to validate email, phone and street address. How would I go about validating these 3 cases in this code: <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Week 10: Regular Expressions</title> <style type="text/css"> span { padding: 2px; } .success { color: #008000; background: #99cc99; } .failure { color: #ff0000; background: #ff9999; } </style> <script type="text/javascript"> function validateInput(form) { var result0 = document.getElementById('result0'), result1 = document.getElementById('result1'), result2 = document.getElementById('result2'),...
<html> <head> <title>Sign Up page</title> <form name="validationForm" method="post" onsubmit="return checkvalidation()"> <!--div class--> <div class="formvalidation"> <label>Your first Name</label> <span id="showname"></span> <!--label for firstname--> <input type="text" name="firstname" class="formsignup" id ="firstn" placeholder="Enter your Name"> <br><br> <!--lastname--> <label>Your last Name</label> <span id="showlname"></span> <input type="text" name="lastname" class="formsignup" id="lastn" placeholder="Enter your last Name"> <br><br> <!--email--> <label>Your Email</label> <span id="showemail"></span> <input type="email" name="emailid" class="formsignup" size="45" id="emailn" placeholder="Enter your Email"> <br><br> <input type="submit" value="send"> </div> </form> <script> function checkvalidation(){ var name = document.forms["validationForm"]["firstname"].value; var lname...
Hello! I am to create a .js file that allows the paragraph on the bottom of the page to update with what the user enters. I need to modify the given HTML to recognize the javascript file that I am to make from scratch. The HTML file and other details are below: Use the given HTML to add functionality to an interactive form that generates an invitation to volunteers for an event. The file will have the following invitation message...
How can I connect the html forms to php --> mysql database <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h2> <center> Average: </h2> <h3> <center> Rate the following: </h2> <h3> <center> Rating Criteria: <br> Developing (0-5), Competent (6-10), Accomplished (10-15); </h3> <center> Judge Name: <input type="text" id="judge"> <br> <br> <center> 1. Articulate requirements and design of the project: <input type="text" id="num1"> <br> <br> 2. Plan the solution and implement the project: <input type="text" id="num2"> <br> <br> 3....
This question relates to Javascript. Could you please show me why my code is not working? I want to create 2 text boxes and have the user enter something into both of them. If the text entered into both boxes is identical the a green message is made visible. If the boxes are different a red message is made visable. Please keep it simple because I am new to javascript. <html> <head> <title></title> <script>...
This question relates to Javascript. Could you please show me why my code is not working? I want to create 2 text boxes and have the user enter something into both of them. If the text entered into both boxes is identical the a green message is made visible. If the boxes are different a red message is made visable. Please keep it simple because I am new to javascript. <html> <head> <title></title> <script>...
can you do this js and html question. ill give you the html and
js and css files just fix my error use if and else like what i did
dont use switch just fix my erorr.
<!DOCTYPE html>
<!-- Webpage HTML document for Lab 4.
Authors: Amirhossein Chinaei, Andriy Pavlovych
For: EECS 1012, York University, Lassonde School of Engineering
-->
<html lang="En">
<head>
<meta charset="UTF-8">
<!-- title for web page -->
<title> EECS1012: Lab 4 - Computational Thinking
</title>...
Javascript on implementing dynamic calculation using a form. How do I get the text in the yellow box to display "You can play FGO!" when all yeses are checked, or "You cannot play FGO!" otherwise? <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Form</title> <link rel="stylesheet" href="css\survey.css"> <script src="js\survey.js"></script> </head> <body> <form id="part1"> <fieldset> <legend>Can you play FGO?</legend> <p>Do you have a smartphone?: ...