Cant remove childnode without making childNode (message) not showing at all - javascript

I've have tried alot of different ways with removing the child and nothing has worked so faar, well it has to some degree, either i have no messages or i keep getting message that just add to the span without deleting the other
Tried reading up on how to remove the child, and have tried every different ways i've found to remove it, my code might be wrong on creating the child and append it etc. since it's the first time i use this way. Been trying with a while loop to remove, and the one that is already outcommented in the code, and with firstChild. and with different names instead of msg.
My code looks like this in my script:
function validateName(input, id)
{
var res = true;
var msg = document.getElementById(id);
var error = document.createElement("span");
var errorMsg = "";
if (input == "" || input < 2) {
res = false;
// removeChildren(msg);
errorMsg = document.createTextNode("Input is to short!");
error.appendChild(errorMsg);
id.appendChild(error);
}
if (input >= 2 && input.match(/\d/)) {
res = false;
// removeChildren(msg);
errorMsg = document.createTextNode("Name contains a number!");
error.appendChild(errorMsg);
id.appendChild(error);
}
if (input >= 2 && !input.match(/\d/)) {
res = true;
// removeChildren(msg);
}
return res;
}
My small test page:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script src="Validator.js"></script>
<script>
function v1(e,id) {
if(validateName(document.form1.namefield.value, id) == false) {
document.getElementById("be").src="NotOkSmall.jpg";
}
if(validateName(document.form1.namefield.value) == true) {
document.getElementById("be").src="OkSmall.jpg";
}
}
</script>
</head>
<body>
<h1>Validation testing, HO!</h1>
<form name="form1" action="submit">
<div id="div1">
<input type="text" name ="namefield" id="f1" onkeydown="v1(be, div1)" >
<image id="be" src="NotOkSmall.jpg" alt="OkSmall.jpg" />
</div>
<input type="button" value="GO" onClick="v1(be)">
</form>
</body>
</html>
If anyone have any ideas to make it work I for one, would be a very happy guy :), as i have said before i am not even sure the creation of child is the correct way in this case. but as it works when i have removed removeChildren, it does write the correct messages, just dont delete any of them. So something must work..
Thanks.

You had some errors in your code like id.appendChild(error); where you had to use msg.appendChild(error);. Anyway I don't see a need to append/remove child nodes in this case. Just use hidden error placeholder and show it when you want to display an error message.
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP Page</title>
<script src="Validator.js"></script>
<script>
function v1(imgId) {
var img = document.getElementById(imgId),
val = document.form1.namefield.value;
img.src = img.alt = validateName(val)
? "OkSmall.jpg"
: "NotOkSmall.jpg";
}
</script>
</head>
<body>
<h1>Validation testing, HO!</h1>
<form name="form1" action="submit">
<div id="div1">
<input type="text" name ="namefield" id="f1" onkeyup="v1('be');" >
<image id="be" src="NotOkSmall.jpg" alt="NotOkSmall.jpg" />
<span id="error-message" class="invis"></span>
</div>
<input type="button" value="GO" onClick="v1('be');">
</form>
</body>
</html>​​​​​​​​​
CSS:
​.invis {
display: none;
}​
JavaScript:
function validateName(input) {
var res = true,
errorMsg,
errorContainer = document.getElementById('error-message');
if(input.length < 2) {
res = false;
errorMsg = "Input is to short!";
}
if(input.length >= 2 && /\d/.test(input)) {
res = false;
errorMsg = "Name contains a number!";
}
if(res) {
errorContainer.style.display = 'none';
} else {
errorContainer.innerHTML = errorMsg;
errorContainer.style.display = 'inline';
}
return res;
}​
DEMO

Related

Having error in sign up and sign in for blogging websites

First of all, I'm a beginner so, I don't know much but, I expect you would help me.
I've made a website for blogging where you sign up and it redirects you to the main page. Here it should tell me if I already signed up once.
I should get an alert if I have signed up before and it should check pass but, here I am not getting it like that. In fact, it redirects even if I have entered the wrong password for the username.
I don't know why this happens...
Signup.html :
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href= "blog.css">
<meta charset="UTF-8">
<title>Page title</title>
</head>
<body>
<div class="Title" align="center">
<br>
<hr>
<h1> InFinite Blogging</h1>
<hr>
<br>
</div>
</div>
<form id="form" action="signup.html" method="post">
<div class="signup">
<input type="text" id="username" placeholder="username">
<br>
<input type="password" id="password" placeholder="password" >
<button id="next" onclick="check()">sign up </button>
</div>
</form>
<script src="blog.js"></script>
</body>
</html>
Blog.js :
var avail = false;
localStorage.setItem(0, 'admin');
localStorage.setItem('admin', '12345678');
function check() {
var user = document.getElementById('username').value;
var pass = document.getElementById('password').value;
var max1 = Object.keys.length;
var max = Object.keys.length-1;
if( user == "" || user== null || pass ==""|| pass==null ) {
alert("enter your name and password, first.");
document.getElementById('username').focus;
document.getElementById('password').focus;
return false;
} else if(pass.length<8) {
alert("Password must contain 8 characters.");
document.getElementById('username').focus;
document.getElementById('password').focus;
return false;
} else {
for (i = 0; i <= max; i++) {
if (user == localStorage.getItem(i)) {
alert("it looks like you already have account 😃.");
avail=true;
PASS = localStorage.getItem(user);
if (pass!=PASS) {
alert("Please, check your password and write correctly.");
} else if(pass==PASS) {
avail='correct';
}
}
}
if(avail!=true) {
localStorage.setItem(max1, user)
localStorage.setItem(user, pass)
document.getElementById("form").action = 'main.html';
} else if(avail=='correct') {
document.getElementById("form").action = 'main.html';
}
}
}
if(avail == false){
localStorage.setItem(max1, user)
localStorage.setItem(user, pass)
document.getElementById("form").action = 'main.html';}
or
else if(pass==PASS) {
confirmed='correct';
}
There are 2 possible solutions:
check for if(avail == false)
use another variable when the password has been confirmed.
By redeclaring avail = "correct", you are changing the typeof(avail) from boolean to string. Avail will never be true because it's no longer a boolean.

How to make a speicific text value respond with different text

I'm trying to get specific value back when I write "h" but it won't, and I don't know why. I have tried searching for the problem but could not find the solution, I don't know if I am just being dumb.
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>BOING</title>
<script type="text/javascript">
function res() {
var a = getElementById("a").value;
var c;
if ( a == f ) {
c = "Hello";
}
else if ( a == j) {
c = "Hi";
}
c = document.querySelector("bleh").value;
}
</script>
</head>
<body class="bd" >
<form class="form" action="index.html">
<input id="a" type="text" name="h" pattern="Write Here" />
<button id="b" type="button" name="button" onclick="res()">Ask</button>
</form>
<div class="blah" id="bleh"></div>
</body>
</html>```
if you want to change text of DIV 'bleh' to respond to user when writing predefined message like Hello or Hi
f="Hello";j="Hi";
function res() {
var a = document.getElementById("a").value;
var c="";
if ( a == f ) {
c = "Hello";
}
else if ( a == j) {
c = "Hi";
}
document.querySelector("#bleh").innerHTML=c;
}
https://jsfiddle.net/rkv88/kjuox029/11/
modification:
1 to put text into DIV element you must use .innerHTML property
2 defined j & f
you can use document.getElemntById("bleh") instead of document.querySelector("#bleh").innerHTML=c;

Validation of a Simple Form

In process of learning JS. Trying to learn how to loop through an entire form, and have errors pointed out.
This is a hodgepodge that I cobbled together from various tutorials online.
Clearly it's not working.
What I am trying to do is to get the forms' entire collection of its elements, and loop through it, and anything that's blank, to have its error message printed somewhere on the same screen, be it above the form or underneath the textboxes itself.
This is what I have so far. Any help in getting this to work, or at least the concept of what I outlined? Simple and bite-size explanations if possible would be appreciated.
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<form method="post" action="" id="myForm">
<div id="validation"></div>
<p><label>Name<br><input type="text" name="Name"></label></p>
<p><label>Email<br><input type="text" name="Email"></label></p>
<p><input type="submit" value="Submit"></p>
</form>
<script>
var formsCollection = document.forms[0];
for (var i = 0; i < formsCollection.elements.length; i++) {
if (formsCollection.elements.value.length == 0) {
form.elements.input.border = "1px solid red";
form.Name.style.backgroundColor = "#FFCCCC";
}
return true;
}
document.getElementById("myForm").onsubmit = function () {
return Validate(this);
};
</script>
</body>
</html>
EDIT
<script>
function Validate() {
var formsCollection = document.forms[0];
for (var i = 0; i < formsCollection.elements.length; i++) {
if (formsCollection.elements[i].value.length == 0) {
form.elements.input.border = "1px solid red";
form.Name.style.backgroundColor = "#FFCCCC";
}
return true;
}
document.getElementById("myForm").onsubmit = function () {
return Validate(this);
};
}
</script>
Full ans:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<form method="post" action="" id="myForm">
<div id="validation"></div>
<p><label>Name<br><input type="text" name="Name"></label></p>
<p><label>Email<br><input type="text" name="Email"></label></p>
<p><input type="submit" value="Submit"></p>
</form>
<script>
document.forms[0].onsubmit= function() {
var form = document.forms[0];
for (var i = 0; i < form.elements.length; i++) {
if (form.elements[i].value.length == 0) {
console.log(form.elements[i]);
form.elements[i].border = "1px solid red";
form.elements[i].style.backgroundColor = "#FFCCCC";
return false;
}
}
}
</script>
</body>
</html>
Several problems.
There seems to be no i in your for loop usage.
Try if (formsCollection.elements.value.length == 0) {
to if (formsCollection.elements[i].value.length == 0) {
Where is your Validate function?
Wrap var formsCollection ... END_OF_FOR_LOOP with function Validate(){ and }

Captcha making, I think it is a logical error

I am having a problem in my code, I made a very simple newbie type captcha using Javascript the following is the my code.
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<h1>Testing captcha</h1>
<hr>
<img id="firstImage" img src="pictureOne.jpg">
<input type="text" id="firstInput"></input>
<button type="button" onclick="checker()">Confirm</button>
<hr>
<p id="cone">Please type what you see in this picture, This is a captcha to prevent over-spamming</p>
</body>
<script>
function checker() {
var checkPic = document.getElementById('firstImage').src = 'pictureOne.jpg'
var takePic = document.getElementById('firstInput').value;
checkPic.toString()
if (checkPic === "pictureOne" && takePic === 'c' ) {
document.getElementById('firstImage').src = 'pictureTwo.jpg';
alert("Please confirm the second captcha");
} else if (checkPic === 'pictureTwo.jpg' && takePic === 'u') {
alert("Ready to download.")
}
}
</script>
</html>
How the captcha will work? Well i tried to make it simple, just like on completing the first captcha the second image will appear and then after finishing that captcha a certain task will be shown. The issue is that the code is not working. I don't know if my condition statements have problem or what so ever please help me. I am stuck in this like for 7 hours.
you have several problems in you code. I first try to fix this problems.
remove unused attribute img from <img id="firstImage" img src="pictureOne.jpg">
remove = 'pictureOne.jpg' from var checkPic = document.getElementById('firstImage').src = 'pictureOne.jpg' to get the real content of element #firstImage instead of setting it every time to pictureOne.jpg
remove line checkPic.toString(). Its not needed (and missing a ; at the end)
use == instead of === because === will test if both sides are the same thing and not only equal. Example: define i=5 and x=5 --> i==x is true but i===x is false and i===i is true
use .endsWith(" to compare your image locations because they will start with http://xyz.abc/ and you only have to check the end
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<h1>Testing captcha</h1>
<hr>
<img id="firstImage" src="pictureOne.jpg">
<input type="text" id="firstInput"></input>
<button type="button" onclick="checker()">Confirm</button>
<hr>
<p id="cone">Please type what you see in this picture, This is a captcha to prevent over-spamming</p>
</body>
<script>
function checker() {
var checkPic = document.getElementById('firstImage').src;
var takePic = document.getElementById('firstInput').value;
if (checkPic.endsWith("pictureOne.jpg") && takePic == 'c' ) {
document.getElementById('firstImage').src = 'pictureTwo.jpg';
alert("Please confirm the second captcha");
} else if (checkPic.endsWith('pictureTwo.jpg') && takePic == 'u') {
alert("Ready to download.")
}
}
</script>
</html>
Now we can talk about the CAPTCHA or is your question already solved?
Try this, you are using the full url of the image, which is not always the same as "pictureOne.jpg", you need get the substring of the url from the end.
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<h1>Testing captcha</h1>
<hr>
<img id="firstImage" src="pictureOne.jpg">
<input type="text" id="firstInput"/>
<button type="button" onclick="checker()">Confirm</button>
<hr>
<p id="cone">Please type what you see in this picture, This is a captcha to prevent over-spamming</p>
</body>
<script>
function checker() {
alert(123);
var checkPic = document.getElementById('firstImage').src;
var takePic = document.getElementById('firstInput').value;
checkPic = checkPic.substring(checkPic.lastIndexOf('/')+1);
if (checkPic === "pictureOne.jpg" && takePic === 'c') {
document.getElementById('firstImage').src = 'pictureTwo.jpg';
alert("Please confirm the second captcha");
} else if (checkPic === 'pictureTwo.jpg' && takePic === 'u') {
alert("Ready to download.")
}
}
</script>
</html>

JavaScript Quiz Not Working

I am trying to make a little quiz to study.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Quiz</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
</head>
<body>
<div id="question"></div>
<form>
<input type="text" id="answer">
<button onClick="check()">Check</button>
</form>
<script>
var spanish = new Array("Hola", "Sí", "No");
var english = new Array("Hello", "Yes", "No");
var x = Math.floor(Math.random()*3);
document.getElementById("question").innerHTML = english[x];
var attempt = document.getElementById("answer");
var correct = spanish[x];
function check(){
if(attempt == correct){
alert("Correct");
}else{
alert("Fail");
};
};
</script>
</body>
</html>
It is returning Fail no matter if I am right or not. Do you know how to fix this? I looked around but could not figure out what is wrong. Thanks.
You need to check for attempt upon clicking the button. You're currently checking it on page load (so it's always an empty string, which is causing correct == attempt to be false). Also, you need to grab the value, not the element.
So, change your function to:
function check() {
var attempt = document.getElementById("answer").value;
if (attempt == correct) {
alert("Correct");
} else {
alert("Fail");
};
};
See here: http://jsfiddle.net/ykZHa/

Categories

Resources