-1

I am trying to make a simple string manipulation program, but I am running into problems.

WHAT THE PROGRAM SHOULD DO:

  • enteredname field must have at least one space but not in the first position.
  • enteredname field must contain 'miller' in anycase somewhere.
  • State field must be only two characters long.
  • Zip field must start with '45'
  • Lastly, streetaddress field is not required to contain the word street, but if it does, it is to be changed to 'Street'.

NOT WORKING:

Currently, everything works except the 'street' name check.

ERROR LOCATION:

if (streetaddress.toLowerCase().indexOf("street") == -1)

Current code:

//need to initialize to empty strings
var enteredname = "";
var streetaddress = "";
var city = "";
var state = "";
var zip = "";

function ValidateandDisplay() {
  enteredname = document.getElementById("NameTextBox").value;
  streetaddress = document.getElementById("StreetAddressTextBox").value;
  city = document.getElementById("CityTextBox").value;
  state = document.getElementById("StateTextBox").value;
  zip = document.getElementById("ZipTextBox").value;

  var isValid = CheckEntries(); // call isValid function here that will
  //                  perform all validation and return with a true or false from CheckEntries
  if (isValid) {
    //string to display
    var correctentries = enteredname + "<br/>" +
      streetaddress + "<br/>" + city + ", " + state + " " + zip;
    document.getElementById("AddressDiv").innerHTML = correctentries;
  }

}

function CheckEntries() {
  //perform all checks here
  //use separate ifs to determine each validation requirement
  // alerting the user to the particular problem if something didn't
  // pass validation and returning with a false
  // ALL of your validation MUST be above the return true statement below
  // it will only get to THIS return if all validation (conditions) were ok
  if (enteredname[0] == ' ')
        {
            alert("First position in name field can not be a space.")
            return false;
        }
        if (enteredname.indexOf(" ") == -1)
        {
            alert("no spaces found in entry.")
            return false;
        }
        if (enteredname.toLowerCase().indexOf("miller") == -1)
        {
            alert("miller is not in name field.")
            return false;
        }
        if (state.length != 2)
        {
            alert("State field must be only two characters long.")
            return false;
        }
        if (zip[0] != '4' || zip[1] != '5')
        {
            alert("Zip field must start with 45.")
            return false;
        }
        if (streetaddress.toLowerCase().indexOf("street") == -1)
        {
            streetaddress.replace("street", "Street");
            return true;
        }
        else
           return true;

    }
Name: <input id="NameTextBox" type="text" /> FirstName LastName with a space between<br /> Street Address: <input id="StreetAddressTextBox" type="text" /> <br /> City: <input id="CityTextBox" type="text" /> <br /> State: <input id="StateTextBox" type="text"
/> <br /> Zip: <input id="ZipTextBox" type="text" /> <br />
<input id="Button1" type="button" value="Validate Entries" onclick="ValidateandDisplay()" />
<div id="AddressDiv">If entered correctly, your address will display here.</div>

<input id="Button1" type="button" value="Split a String" onclick="SplitThis()" />
  • So what would be the question? – Balastrong Sep 21 '21 at 17:11
  • You can't use negative indexes in JavaScript to index from the end. Use the `startsWith()` and `endsWith()` methods to test the beginning and end. – Barmar Sep 21 '21 at 17:11
  • How do I fix: checking the first character field in name for a space, the state two character limit, the zip starting with '45', and the 'street' name check. – jeantaylor2021 Sep 21 '21 at 17:12
  • Code and question updated – jeantaylor2021 Sep 21 '21 at 17:21
  • one thing that I don't understand is **Lastly, streetaddress field is not required to contain the word street** – callmenikk Sep 21 '21 at 17:42
  • Yes, because some places are called Avenues or Roads. So, if Street or STREEt or any version of the word street is entered, how do I replace their version of STREEt with Street? – jeantaylor2021 Sep 21 '21 at 17:45
  • This is how, But you will need to split the value, and via for loop you must check which array index contains `street` as lovercase and after that you must convert that string in lowercase and use that source [How can I capitalize the first letter of each word in a string using JavaScript?](https://stackoverflow.com/questions/32589197/how-can-i-capitalize-the-first-letter-of-each-word-in-a-string-using-javascript) – callmenikk Sep 21 '21 at 17:48
  • I just edited to code, can you view the streetaddress if. It still does not work. – jeantaylor2021 Sep 21 '21 at 17:49

1 Answers1

0
  1. String (array) indices start at 0
  2. state.length > 2 ==> shoulf be "not equal"
  3. (zip[-1] != '4' && zip[0] != '5') => It must be OR. And the indices are wrong
  4. You are missing quotes in last if statement
Tigl
  • 16