开发者

Problems with using the jQuery watermark and rails validations

开发者 https://www.devze.com 2023-01-17 19:05 出处:网络
Ok so i have a text field that uses this jQuery watermark plugin which is really easy to implement. The problem comes into play when i need to validate this field with a rails validation. For example.

Ok so i have a text field that uses this jQuery watermark plugin which is really easy to implement. The problem comes into play when i need to validate this field with a rails validation. For example..

def validate 
  errors.add(:price, "should be at least $0.01") if price < 0.01 
end

I am using the plugin like this

 $("#request_price").Watermark("Not at this time");

The params are coming in like this

"price"=>"No开发者_运维知识库t at this time",

I need to make sure if the user enters a price it is over 1 cent and if they leave the field blank that nothing goes in the db. But how do i stop the params from coming in


So, if you really wanted to control this in the views, you could use a plugin which honors placeholder text (standard in HTML5), versus this home-spun solution. Here's such a plugin.

Then, you could bind an event to the form submit, and if the value is the placeholder value, set the value to "". Something like

$('form').submit(function(){
$(this).find('input').each(function(){
    if( $(this).val() == $(this).attr("placeholder") ){
        $(this).val("");
    }
});
});
0

精彩评论

暂无评论...
验证码 换一张
取 消