views:

54

answers:

1

Ok so i have this problem i am trying to address. I have this textfield with the id of request_money

<input id="request_money" name="request[money]" size="30" type="text">

In this textfield i have a watermark plugin that i am using with the text "Not now" which is happening via jQuery

$("#request_money").Watermark("Not Now");

This works fine but the problem arrises when i submit the form and this field is only supposed to be a numeric field, but when i submit it blank it takes the text "Not now" and sends it in. I tried a few ways to handle this...First i tried with jQuery like this...

$('form').submit(function(){
if($("#request_money").val("Not now")){
$("#request_money").val("")
}
});

but for some reason it even clears out all the numeric values also...seems to always be true, not sure why

and second i tried in the rails model

using the errors

def validate 
  if money == "Not now" 
  money = ''
end

And rails did not like that...any suggestions

+2  A: 

Your if() check is setting the value, it needs to get the value and check it against the default, like this:

if($("#request_money").val() == "Not now"){
  $("#request_money").val("")
}

Depending on how your watermark plugin works, you may be able to do this in a generic way for all watermarked elements...if you can't I'd suggest changing the plugin to allow it, or example storing the watermark text in data, then you could do something like:

$(".watermark").val(function(i, v) {
  return v == $.data(this, "watermark") ? "" : v;
});

If your plugin stored the watermark text as the "watermark" $.data() value, and say applied a class to easily identify the elements, you could do the check above to easily loop though and clear any with a default value just before submission. It's just a suggestion, not sure which plugin or how much control you have over it, but it would solve this common problem with value-based watermarks.

Nick Craver
not sure if that will work...i updated my question with the plugin
Matt