jquery validate - replace commas in field before validation occurs

百般思念 提交于 2020-01-14 12:32:27

问题


It appears that even with the current validate plugin you can't have a comma in a value if you want to validate using min. I found on github where the patch was submitted several months ago (11 months) to modify the source .js file but it's still not in release.

So instead of modifying my source .js file I'm trying to figure out how to replace the comma prior to validation.

I tried something like this:

$("#amount").blur(function(){
    var val = $(this).val().replace(/\,/g,'');
    $(this).val( val ); 
});
$("#transferForm").validate({
    .......
});

But it runs validate first, fails because of min and then replaces the value. I don't necessarily want it to fail validation and tell the user to remove commas - I just want it to remove the comma before running the min rule.


回答1:


Don't even let them enter comma's

$("#amount").keyup(function(){
    var val = $(this).val().replace(/\,/g,'');
    $(this).val( val ); 
});

Demo: http://jsfiddle.net/calder12/tdxKv/




回答2:


Try this http://jsfiddle.net/MWjZr/

$(function(){
  $("input[id=sometexboxid]").each(function(){
      this.value=this.value.replace(/,/g, "");
  }).on('keyup', function(){
      this.value=this.value.replace(/,/g, "");
  })
});

For more explanation please refer http://davidwalsh.name/javascript-replace article.



来源:https://stackoverflow.com/questions/15415814/jquery-validate-replace-commas-in-field-before-validation-occurs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!