Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jenny Lane
Full Stack JavaScript Techdegree Student 6,731 PointsHow to parseint the value of a text input field?
I know there is a video on this if you just want to point me to that. I have an input with type="text". I want to set a max of 20 for the number that can be added to the input field. I cannot change the type to number due to the way the information syncs with the database. So I need to change the string value that is entered by the frontend user to a number so that I may set a max on what can be entered.
Any help would be greatly appreciated. The html for the input field is below:
<input name="student" id="studentQuant" maxlength="2" onfocus="javascript:this.select();" onmouseup="javascript:if(event && event.preventDefault){event.preventDefault();}else{return false;}" value="0" buddy="10181" going="3660" type="text">
I tried this: $( "input#studentQuant" ).change(function() { $("parseInt($('input#studentQuant);").prop('max',10); });
2 Answers
Abraham Juliot
47,353 PointsJust give your input element a type attribute of number and a max attribute & value.
<input type="number" max="5">
HTML5 will validate the user input -- no javascript required
Jenny Lane
Full Stack JavaScript Techdegree Student 6,731 PointsHi Abraham, Thank you for your reply, I think I mentioned this in my initial question but I cannot change the type to number due to the way the information comes into the database, that field has to remain text.
Abraham Juliot
47,353 PointsSorry I overlooked that. In any case, you can get and parse the value of your input to a string before form submission.
Jenny Lane
Full Stack JavaScript Techdegree Student 6,731 PointsJenny Lane
Full Stack JavaScript Techdegree Student 6,731 PointsWould you mind elaborating? I need the input type to remain "text" or the database will not not sync the information properly. In order for the sync to work the type has to match so I am trying to parseint the value of the input that the user enters without changing the input field type.
Abraham Juliot
47,353 PointsAbraham Juliot
47,353 Pointsjust change your jQuery selector to get the value and then parse it to an integer
var myInteger = parseInt( $('#studentQuant').val() )The html max attribute does not work on type="text", so you would have to validate your max input value with javascript.