Ben Smith
Ben Smith

Reputation: 43

Form Input Not Accepting Spaces

I've got a form using JavaScript that sends a user to a different page depending on their input using "if/else" statements. However, the input field will not accept spaces. It will accept every character except spaces. I can't seem to figure out what is causing this.

EDIT: I have added the HTML for the entire form, including the submit button.

EDIT 2: I have also updated my JS to reflect the 'switch' statements, rather than else if, as recommended by Mister Jojo.

EDIT 3: Just to clarify, if I past spaces in, it will work. But if I just type them it will not show a space.

Below is the HTML, CSS, and JS that goes along with the input form.

<!-- This is the HTML for the entire form -->

    <<form onSubmit="return checkAnswer();">
                                              <input class="nice-select wide" placeholder="Enter Your State" id="state" type="text" maxlength="55" class="box" autofocus />
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!--<p>You have to pay: <span>$0</span></p>-->
                            <div class="submit_btn">
                                <button class="boxed-btn3" value="SUBMIT" type="submit">Continue</button>
                            </div>
                          </form>

<!-- This is the script that is in the head of the document controlling the redirect. -->

    <script>
    function checkAnswer(){
      var response = document.getElementById('state').value;

      if (response == "Kentucky")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "kentucky")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "KY")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "ky")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "New York")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "new york")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "NY")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "ny")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "Washington")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "Washington St")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "Washington ST")
        location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "Washington st")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "washington")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "washington state")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "Washington State")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "Washington state")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "washington State")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "wa")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "WA")
      location = 'https://linxdalton.com'; // 'right.html';

      else
      location = 'https://google.com'; // 'wrong.html';
      return false;
    }
    </script>

Below is all of the CSS that is associated with the input element.

.nice-select::after {
  content: "\f0d7";
  display: block;
  pointer-events: none;
  position: absolute;
  right: 30px;
  top: 50%;
  transition: all 0.15s ease-in-out;
  width: 5px;
  font-family: 'fontawesome';
  color: #ddd;
  transform: translateY(-50%);
  font-size: 16px;
  right: 25px;
  color: #AAB1B7;
  height: auto;
  margin-top: 0;
}


-webkit-writing-mode: horizontal-tb !important;
    text-rendering: auto;
    color: -internal-light-dark-color(black, white);
    letter-spacing: normal;
    word-spacing: normal;
    text-transform: none;
    text-indent: 0px;
    text-shadow: none;
    display: inline-block;
    text-align: start;
    -webkit-appearance: textfield;
    background-color: -internal-light-dark-color(white, black);
    -webkit-rtl-ordering: logical;
    cursor: text;
    margin: 0em;
    font: 400 11px system-ui;
    padding: 1px;
    border-width: 2px;
    border-style: inset;
    border-color: initial;
    border-image: initial;
}

-webkit-tap-highlight-color: blue;
    background-color: #fff;
    border-radius: 5px;
    border: solid 1px #e8e8e8;
    box-sizing: border-box;
    clear: both;
    display: block;
    float: left;
    font-family: inherit;
    font-size: 14px;
    font-weight: normal;
    height: 42px;
    line-height: 40px;
    outline: none;
    padding-left: 18px;
    padding-right: 30px;
    position: relative;
    text-align: left !important;
    -webkit-transition: all 0.2s ease-in-out;
    transition: all 0.2s ease-in-out;
    white-space: nowrap;
    width: auto;
}


    width: 100%;
    height: 50px;
    line-height: 50px;
    border-radius: 5px;
    color: #7A838B;
    margin-bottom: 15px;


That's all the code connected with that input element. Let me know if you can find any reason that it wouldn't be accepting spaces!

Thanks!

Upvotes: 0

Views: 1431

Answers (3)

bedkan
bedkan

Reputation: 39

I faced to same problem on Angular project, due to following script

fromEvent(window, 'keydown')
  .subscribe((event: any) => {
  if ([32, 37, 38, 39, 40].indexOf(event.keyCode) > -1) {
    event.preventDefault();
  }
});

Since space keycode is 32 , it is prevent to typing space in all input. Delete this script or replace window with a nativeElement to apply script on specific component.

Upvotes: 0

Mister Jojo
Mister Jojo

Reputation: 22320

better to code https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

 function checkAnswer(){
  let response = document.getElementById('state').value;

  switch (response) {
    case 'Kentucky':
    case 'kentucky':
    case 'KY':
    case 'ky':
    case 'New York':
    case 'new york':
    case 'NY':
    case 'ny':
    case 'Washington':
    case 'Washington St':
    case 'Washington ST':
    case 'Washington st':
    case 'washington':
    case 'washington state':
    case 'Washington State':
    case 'Washington state':
    case 'washington State':
    case 'wa':
    case 'WA':
    location = 'xyz.com'; // 'right.html';
      break;

    default:location = 'https://google.com'; // 'wrong.html';
      break;
  }

  return false;
}

Upvotes: 1

Ben Smith
Ben Smith

Reputation: 43

I've solved it! It was a script that I had linked to the page that was affecting spaces, I'm not completely sure what the contents of the script were that affected spaces, but nonetheless the issue is solved.

Upvotes: 0

Related Questions