1

I'm having a small issue with my coding. I am building this in regular, old Notepad (please don't judge) as a personal feat of my own. Along with this, I want to use only Javascript and HTML, along with CSS to build this.

My code is as follows:
HTML

<html>
<h1>Please join our email list</h1>
<body>
<script language="javascript" src="country.js"></script>
<form name="myForm">
<label for="email_address1">Email Address:</label>
<input type="text" id="email_address1" name="email_address1">
<span id="email_address1_error">*</span><br></br>

<label for="email_address2">Re-enter Email Address:</label>
<input type="text" id="email_address2" name="email_address2">
<span id="email_address2_error">*</span><br></br>

<label for="first_name">First Name:</label>
<input type="text" id="first_name" name="first_name">
<span id="first_name_error">*</span><br></br>

<label for="select_country">Select your country:</label>
<select id="myList" onchange="choice(),validate()">
<option value="0"></option>
<option value="1">United States</option>
<option value="2">Canada</option> 
</select><span id="country_error"> *</span><br></br>
<span for="res_code" id="res_code_lbl">Zip/Province Code: </span><input type="text" id="res_code" name="res_code"><span id="res_code_error"> *</span>
<br></br>
<button type="button" id="joinButton" onclick="validate(),joinList()">Join Our List</button>
</form>
</body>
</html>

Javascript

function validate(){
var e = document.getElementById("myList");
var f = e.options[e.selectedIndex].value;
if(f == 0)
{
document.getElementById("country_error").innerHTML="Choose a country!";
}
else{
document.getElementById("country_error").innerHTML="*";
}
}

function choice(){
var e = document.getElementById("myList");
var f = e.options[e.selectedIndex].value;
if(f == 1){
document.getElementById("res_code_lbl").innerHTML="Zip Code: ";
}
else if(f == 2){
document.getElementById("res_code_lbl").innerHTML="Province Code: ";
}
}

function joinList(){
var e1 = document.getElementById("email_address1").value;
var e2 = document.getElementById("email_address2").value;
var n = document.getElementById("first_name").value;
var c = document.getElementById("res_code").value;
if(e1 == null){
document.getElementById("email_address1_error").innerHTML="Enter your email address!";
}
else{ 
document.getElementById("email_address1_error").innerHTML="*";
if(e1 != e2){
document.getElementById("email_address2_error").innerHTML="Your email address must match!";
}
else{
document.getElementById("email_address2_error").innerHTML="*";
if(n == null){
document.getElementById("first_name_error").innerHTML="Enter your first name!";
}
else{
document.getElementById("first_name_error").innerHTML="*";
if(c == null){
document.getElementById("res_code_error").innerHTML="Enter a valid State/Province Code!";
}
else{
document.getElementById("res_code_error").innerHTML="*";
if((e1 != null) && (e1 = e2) && (n != null) && (c != null)){
location.href='countryjoin.html';
}
}
}
}
}
}

I know this is a lot so I'll spare you the CSS :P Basically I want to validate that none of the inputs are null. When the validate function runs, it seems to allow all the fields through as though they were not null, even though they are, but never initiates the change in location.href. The only field that works correctly is the select list. That will work onchange and onclick with the submit button.

Any feedback is much appreciated! Thank you!

2
  • I guess I should also say that I have tried nesting my if,else statements as well as separating them into separate functions. I had 4 joinList() functions at one point, but those did not work either. I tried having if + if elses, but those wouldn't work. I haven't had much luck so far. Commented Oct 17, 2013 at 17:36
  • Please indent your code, it's very hard (not to say horrible) to read. That will help you and your fellow developers. Commented Oct 17, 2013 at 17:46

1 Answer 1

3

Rather than comparing those values to null, compare them to empty strings, since that is what the values will be if not entered in a textbox. I'd also add a call to trim() to make sure that they haven't entered just spaces.

if (n.trim() === "") {
  //it is empty
}

It would be helpful during debugging to look at what the value of one of those variables is; that way it would be immediately obvious they they are not null, but rather empty strings.

Fiddle to demonstrate

Sign up to request clarification or add additional context in comments.

3 Comments

@Agony No problem. If it helped you, remember to mark it as accepted by clicking the green check mark so it's obvious to future visitors which answer solved the problem!
No worries, I'm watching the timer. "You can accept an answer in 4 minutes." :)
Wow. I wasn't even aware that was a thing. That is quite the function, thank you Mansfield!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.