Prevent Wrong Input in JS After Submitting A Form

Jiangxi123

I am creating a sample MabLibs type thing in HTML and JS. When the person inputs stuff in a field, it will use that to create their own MadLib.

I've done a little research and not finding exactly what I am looking for. Say a person puts 12 in the Name field. How would code that so if this instance does happen, it won't go through and alert "That is not a valid input. PLease type again!" or something along those lines.

The code I am using is below. I am very new to Javascript so I know the format and stuff might be wrong.

<html><head>
<title>
  Mad Libs Story
</title>

<script>
  function getVars() {
    person1 = String(document.getElementById("personOne").value);
    age = Number(document.getElementById("ageOne").value);
    firstAdjective = String(document.getElementById("adjective").value);


    document.getElementById("madLibCreation").innerHTML = "There once was a person named " + person1 + ". She was " + age + " and very " + firstAdjective = ".";

  }
</script>
</head>
<body>
<h3>
Welcome to Mad Libs! Please type in the prompted Information. Then press the submit button. Have fun!   
</h3>  

<p>
  Name of Person in Room: <input type="text" id="personOne">
  </p>
<p>
  Age: <input type="text" id="ageOne">
  </p>
<p>
  Adjective: <input type="text" id="adjective">
  </p>



<input type="submit" value="Get My MadLib Creation!" onclick="getVars();">

<p id="madLibCreation"></p>

</body></html>
Madushanka kahawa

For that, you have to check Name field value is number or not. We can check the value is number or not using isNaN function. This function returns true or false.

isNaN(12)           // falsee
isNaN(-4.5)         // false
isNaN(15-3)         // false
isNaN(0)            // false
isNaN('123')        // false
isNaN('Nuwan')      // true
isNaN('2005/12/12') // true
isNaN()             // true

So, in your code getVars() function change like this

function getVars() {
        var name = document.getElementById("personOne").value;
        if(!isNaN(name) && name.length != 0){
            alert("That is not a valid input. PLease type again!");
        }else{
            person1 = String(document.getElementById("personOne").value);
            age = Number(document.getElementById("ageOne").value);
            firstAdjective = String(document.getElementById("adjective").value);
            document.getElementById("madLibCreation").innerHTML = "There once was a person named " + person1 + ". She was " + age + " and very " + firstAdjective + ".";
        }
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

prevent input from submitting when empty (not in form)

Prevent form submitting when pressing enter from a text input, using Vue.js

Set the focus to input after submitting the form

Clear form input field values after submitting in react js with ant-design

Prevent an input from submitting

Transloading is submitting the wrong form

How to prevent the modal from closing after submitting form

How to prevent redirect after submitting data through a form

Prevent multiple form submit when using JS for submitting

Button submitting twice after adding JS code to prevent multiple clicking

Clear the input box after submitting the form in Angular 4

React setState doesn't update the state after submitting input form

Cannot reset input fields after submitting form in React

How to make pseudo class :invalid apply to an input AFTER submitting a form

IE - How to disable validation detection from input after submitting the form?

Unable to clear the input field after submitting the form in React

React Router redirect after submitting a form using the input value

In PHP form, string input gets converted to 0 after submitting

Form clears after submitting

Attempting to prevent submitting empty form

Prevent form from submitting jQuery

Prevent users submitting a form using Enter but enable enter key on one form input field

Form not submitting after form validation

VUE.js Refreshing img scr after submitting a form

Close a bootstrap modal 5 after asynchronously submitting a form with vue js

How to load table data after submitting the form in react js php

Want to redirect to the URL after successfully submitting the form JS

ajax does not return control under form for js after submitting

Form is not submitting in react js