ga-wdi-boston / full-stack-project

Other
8 stars 64 forks source link

form validation #1027

Closed ryanwk closed 7 years ago

ryanwk commented 7 years ago

When a user updates an exercise or adds an exercise, two fields are required "name" and "weight". I would like to allow users to only input text into name, and integers into weight, with some type of error handling that will warn a user against doing anything otherwise. What I haven't figured out is how to implement the error handling/warning.

As of right now, users can enter text into the "weight" field. Since my back end is expecting integers for weight, the record it creates and displays on the front end appears as '0' instead of whatever text the user placed in the "weight" field. That's better than displaying text where an integer should be, however, I'd like to improve this and cannot figure out how.

I've read through these docs: https://www.w3schools.com/js/js_validation.asp

I need a nudge in the right direction.

cpearce31 commented 7 years ago

You could use the typeof operator to find out whether the input is a string or integer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof

ryanwk commented 7 years ago
<input name="exercise[weight]" type="number" class="form-control" id="inputWeightAdd" placeholder="Exercise Weight (only accepts numbers)">
                  </div>

set the type to number prevents a user from adding numbers to a field that expects text