Ruby date_select conversion

Posted by John
on Wednesday, 19 March 2008

Had a bit of a pain with Ruby's handling of Date/Time values coming from a Date_Select form element, managed to solve it though.

The problem was that in my form...

<%=date_select(:date,'',:start_year => 1950,:include_blank => false, :default => { :year => '1970' })%>

...the value I got back from the submitted field,

params[:date]

became...

'(3i)4(1i)1953(2i)5'

Bit of a mess, obviously I was expecting something a little more legible like 1953-5-4 (yyyy-mm-dd), so I bashed my head against a brick wall trying to work it out, I could have made my life easier with some select_date blocks but I'm a guy how likes to ask 'why' so I perceviered.

Turns out in my travels you can do this to break it into a usable DateTime object..

Time.mktime(params[:date]['(1i)'],params[:date]['(2i)'],params[:date]['(3i)'])

Which will return something much more legible...

Sun May 19 00:00:00 +0100 1968

Or whip it into a method to do exactly what you want...

def convert_date(obj)
  return "#{obj['(1i)']}-#{obj['(2i)']}-#{obj['(2i)']}"
end

<%=convert_date(params[:date])%>

But wait, it doesn't end there, you could use Date.new or Date.civil to parse it into a real date object like...

def convert_date(obj)
  return Date.new(obj['(1i)'].to_i,obj['(2i)'].to_i,obj['(3i)'].to_i)    
end

Which will return...

1967-03-19

Much nicer, just remember to use .to_i otherwise you'll get...

comparison of String with 0 failed

Enjoy!

Request.QueryString + Select Redirect

Posted by John
on Thursday, 14 February 2008

Quick post, to handle parameters in your webpage via javascript, try this...

Request.QueryString with JS

Include the file in your header section then use like...

mypage.html?username=john

With the Javascript...

var name = '';
if (Request.QueryString("username").Count > 0) {
    name = Request.QueryString("username").Item(1);
}

The function returns the querystring as an object, so to determine whether you have any parameters returned count the number of objects. The first of which we want so we use .Item(1) to grab that and stick it in our variable name after which we can give to an alert() popup or whatever we need.

On Select Redirect with JS

Here's another bit, this will force the browser to redirect to another page once the user selects a value from the drop-down box and clicks the button image.

Javascript...

function goto(form) { 
var index=form.select.selectedIndex
    if (form.select.options[index].value.length != 0) {
        location.replace(form.select.options[index].value);
    }
}

Html...

<FORM NAME="form1">
<select name="select" id="select" style="width:90px;">
<option value="http://www.red91.com/">my site</option>
</select>
<img src="button.gif" onclick="goto(form1)" />
</FORM>

Javascript Form Validation

Posted by John
on Friday, 02 November 2007

To quickly validate a user's form input via Javascript, try the following...

Add 'onsubmit' to Form Tag

First add the onsubmit action to your Form Tag, so that when the user clicks the submit button your Javascript function will fire, and more importantly return a value (true=ok, false=bad).

<form action="checkout.php" method="post" onsubmit="return checkform(this);">

Breaking this down, when the user clicks the submit button, the checkform() Javascript function will fire. When this function returns True then the form data will be sent via the Post method to the page 'checkout.php'.

And using 'this' will pass this form to the Javascript function.

The Javascript 'checkform' Function

Now the Javascript function, add this inside the HEAD portion of your web page.

<script language="JavaScript" type="text/javascript">
<!--
function checkform ( form )
{
if (form.myfield.value == "") {
alert("your error message");
return false ;
}
return true ;
}
//-->
</script>

Really simple when you break it down.

The function will fire when the user clicks the Submit button on the form, when that is done the Form will pass it's contents to this Javascript function, expecting a value returned.

Now the 'checkform' function will check the form field 'myfield'. If it's value is '' blank then it returns false, otherwise the function returns true.

Note the Form will only continue if the function returns a True value.