0

I have a form that needs validating using DOM methods and javascript for our web class. I have validated all name, address, email, etc. fields. I have validated the given credit card number using a modulus algorithm. Our next task is to validate the given date (month, year) that is given via a standard option box on the html file. I understand how to retrieve the year (as it will be returned as an integer), however, was curious as to the most efficient way to retrieve and convert the Month into an int. The month is in standard form:

<label for="month">Month:</label>
                <select id="month"  name="month" disabled="true">
                    <option value="0">January</option>
                    <option value="1">Febuary</option>
                    <option value="2">March</option>
                    <option value="3">April</option>
                    <option value="4">May</option>
                    <option value="5">June</option>
                    <option value="6">July</option>
                    <option value="7">August</option>
                    <option value="8">September</option>
                    <option value="9">October</option>
                    <option value="10">November</option>
                    <option value="11">December</option>
                </select>

Easy enough to get the month as a string, but is there some way to get the number back directly using document.getElementById('month') but somehow tagging it with option value +1 so it directly returns the int? Or do I have to do it long hand in the function, ie

January = 1; February=2; etc.?

Thanks for any help.

2
  • 1
    why not just set their values 1 higher? Commented Apr 1, 2011 at 18:22
  • Yeah, but we aren't allowed to make any changes to the source html. However, I guess I am asking how to retrieve their values in numeric form. I am unsure if you can, and if so, the syntax involved. Commented Apr 1, 2011 at 18:24

2 Answers 2

2

You can call the parseInt function. Like so:

// find your selected option
var monthInteger = parseInt(yourSelectedValue) + 1;
// aka Value of "0" gets parsed to 0, then you can add 1.
Sign up to request clarification or add additional context in comments.

Comments

1

If you do:

(document.getElementById('month').value * 1.0) + 1

This should return the month as an integer value. I've done * 1.0, but you could you parseInt. Just makes sure that 1+1 = 2 (not 11)!

1 Comment

Thank you, I thought it would be easy, but we beginners are quite unsure of ourselves!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.