0

I have a month object and a dropdown field. I'm trying to submit an expiration month to the server but it expects the month to be formatted as a 2 digit integer. How do I do that without turning the number into a string?

My code (using AngularJS):

$scope.months = [01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12];

<select
    name="payment"
    ng-model="expiration_month"
    ng-options="m for m in months"
    required>
       <option value="">Month</option>
 </select>

Problem: it's submitting as 1 rather than 01. I've tried value="01", which keeps the 0 but it submits as a string and as soon as I parseInt() it looses the 0 at the front. Help. Please. Thank you.

Brooke Clonts
  • 465
  • 1
  • 10
  • 20

2 Answers2

1

The easiest solution would probably be to have the array of months be strings instead of integers. (i.e. $scope.months = ['01', '02', '03', '04'...]).

Andrew Burgess
  • 5,300
  • 5
  • 30
  • 37
  • yes but then it submits as a string and the post fails because the server won't accept a string value. – Brooke Clonts Feb 10 '16 at 19:42
  • I guess we would need to know how the values were being submitted then. Numbers generally aren't represented with leading zeroes, so I would say alter the server side validation. If submitting as a JSON object, then I'm not sure why the server would be expecting a leading 0 for an integer value. If submitting as URL encoded form data, then it is assumed that basically everything is a string and it's up to the server side to cast those values to their appropriate data types. – Andrew Burgess Feb 10 '16 at 20:11
0

You cannot do that in javascript and php. Either you will keep the value as a string and use parseInt only if you want to validate it without sending an int to the server, or you can make them single digit numbers, pass them to the server and there after your validation you can make it a two digit string again

eltonkamami
  • 5,134
  • 1
  • 22
  • 30