I've read this question and changed my rails model code accordingly: Regular expressions with validations in RoR 4
Here's my regex for my datetime in rails:
/\A((19[7-9][0-9])|(2[0-9][0-9][0-9]))\/((0[1-9])|(1[0-2]))\/(([0-2][0-9])|(3[0-1]))\s(([0-1][0-9])|(2[0-4]))\:([0-5][0-9])\z/
This regex below works in javascript, though:
/^((19[7-9][0-9])|(2[0-9][0-9][0-9]))\/((0[1-9])|(1[0-2]))\/(([0-2][0-9])|(3[0-1]))\s(([0-1][0-9])|(2[0-4]))\:([0-5][0-9])$/
I'm trying to pass a datetime string to them both, but I can't understand why it keeps failing. Here's the string:
2014/12/02 11:06
I thought my model code might be helpful:
validates :start, presence: true, format: { :with => /\A((19[7-9][0-9])|(2[0-9][0-9][0-9]))\/((0[1-9])|(1[0-2]))\/(([0-2][0-9])|(3[0-1]))\s(([0-1][0-9])|(2[0-4]))\:([0-5][0-9])\Z/, :message => "must be in this format: YYYY/MM/DD HH:MM" }
validates :finish, presence: true, format: { :with => /\A((19[7-9][0-9])|(2[0-9][0-9][0-9]))\/((0[1-9])|(1[0-2]))\/(([0-2][0-9])|(3[0-1]))\s(([0-1][0-9])|(2[0-4]))\:([0-5][0-9])\Z/, :message => "must be in this format: YYYY/MM/DD HH:MM" }
Edit: I figured out what the issue is, but I still don't know how to fix it...
For some reason when I submit my form the date 2014/12/24 16:19
is being cut down to just 2014
. This then fails validation and throws the error.
Can anyone help me figure this out?